#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    String to int array conversion in C programming


    Hi
    I need to know how can I convert a string into an int array and then int array into string array.

    void readArr(int MD[], int MQ[])
    {
    int i, b, val;
    int array[4];
    char string1[4];
    char string2[4];
    printf("\nEnter the value of Multiplicant: ");
    gets(string1);
    /* string1 is "0011" and I need it to convert as
    array[4]={0,0,1,1} */

    for (int i=0; i< string1.length; i++)
    {
    array[i] = Integer.ParseInt(string1[i]);
    }
    }

    I am getting problem in my code. I don't know how to write a code for converting int array into string.

    thank you in advance
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
    int i, b, val;
    int array[4];
    char string1[4];
    char string2[4];
    printf("\nEnter the value of Multiplicant: ");
    gets(string1);

    for (i=0; i< strlen(string1); i++)
    {
    array[i] = (int)string1[i]-'0';
    }

    for(i =0;i<strlen(string1);i++)
    {
    printf("%d",array[i]);
    }
    getch();




    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Wow gets()

    Code:
    gets(string1);
    Right from my help files -

    Code:
    Never use gets().  Because it is impossible to tell without knowing the data in
     advance how  many  characters gets()  will  read,  and  because  gets() will
     continue to store characters past the end of the buffer, it is extremely
     dangerous to use.  It has been used to break computer security.  Use fgets()
     instead.
    As for converting int to strings and strings to int, I would check out the functionality of sprintf().
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally Posted by Cprogrmmer
    = Integer.ParseInt(string1[i]);
    That's not C, but rather it's C#. Though the rest of the code is not C# but rather C. That mixing of two different languages only makes your request even more confusing than it already is.

    What are you trying to do?

    Originally Posted by Cprogrmmer
    I don't know how to write a code for converting int array into string.
    Simple, use sprintf. And if you're going to try to do it with a for-loop, then consider sprintf plus strcat.

    Though of course if we could figure out what you're trying to do, we could offer better suggestions.


    And also, use code tags! Eg:
    Code:
    void readArr(int MD[], int MQ[])
    { 
        int i, b, val;
        int array[4];
        char string1[4];
        char string2[4];
    
        printf("\nEnter the value of Multiplicant: "); 
        gets(string1);
        /* string1 is "0011" and I need it to convert as 
        array[4]={0,0,1,1} */
    
        for (int i=0; i< string1.length; i++)
        { 
            array[i] = Integer.ParseInt(string1[i]);
        }
    }
    There is great value in enabling us to read your code.
    Last edited by dwise1_aol; October 22nd, 2012 at 02:12 PM.

IMN logo majestic logo threadwatch logo seochat tools logo