String To Integer Array!!!!

This is a discussion on String To Integer Array!!!! within the C Programming forums, part of the General Programming Boards category; Ok I finally got my program to read in a line as a character array for a string but now ...

  1. #1
    Unregistered
    Guest

    Talking String To Integer Array!!!!

    Ok I finally got my program to read in a line as a character array for a string but now I want to convert that character array into an integer array.

    Its like this:

    if I input 1 2 3 4 5

    I want to put that into an integer array with a[0]=1, a[1]=2, etc.

    Or if I have this: 12 45 6 23 100

    I want to convert that into the integer array like a[0]=12, a[1]=45, a[2]=6, a[3]=23, etc.
    If I can do this then my problem is solved so please help me!

    I guess I need to find the spaces and concatenate anything before that into an array location such as a[4] or something.

    thanks,
    dragoon


    Here is my code so far!

    #include <stdio.h>
    int n;
    char a[200];

    int caten()
    {
    // int *numchar;
    // *numchar = a;

    printf("%s", a);
    // printf("%d", *numchar);
    return 0;
    }


    int main()
    {
    n = 0;
    printf("Enter some integers (maximum of 20):\n");
    while((a[n++]=getchar( )) != '\n' );
    a[n] = '\0';
    if (a[n] == '\0')
    caten();
    // printf("%s", a);
    return 0;

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Code:
    #include <stdio.h>
    
    void foo ( char *line ) {
        int num, i = 0, len;
        while ( sscanf( line, "%d%n", &num, &len) == 1 ) {
            printf( "Number %d is %d\n", i, num );
            line += len;    // step past the number we found
            i++;            // increment our count of the number of values found
        }
    }
    int main ( ) {
        char test[] = "12 45 6 23 100";
        foo( test );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest

    im sorry

    I need to put it into a int array[] and I need to use an int *n for my number to count. So please help me put the string array into the integer array using the *n.

    thnx
    dragoon

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Come off it, you mean adding
    &nbsp; array[i] = num;
    to the middle of the code is too hard?

    At least have a go at it - do some reading, try some stuff out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523

    Whats that mean?

    salem

    while ( sscanf( line, "%d%n", &num, &len) == 1 )
    What does the format specifier %n do? Haven't come across that.

    and if sscanf is doing 2 conversions shouldnt it return 2 ?
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    void writenum(const char *s)
    {
    int k=0;
    int c;
    do
    {
    while( (c=*s++) && (c==' ') );
    if(isdigit(c))
    {
    int i=0;
    while(isdigit(c))
    {
    i=10*i+(c-'0');
    c=*s++;
    }
    printf("index:%d value:%d\n",++k,i);
    }
    else if(c)
    {
    printf("invalid character with ascii value %d found\n",c);
    return;
    }
    }while(c);
    }

    int main(void)
    {
    writenum(" 1 2 3 344 1 44");
    return 0;
    }


    when I copy paste from my editor why does all the formatting get lost ?

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    the program works only for positive integers though
    Originally posted by pinko_liberal
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    void writenum(const char *s)
    {
    int k=0;
    int c;
    do
    {
    while( (c=*s++) && (c==' ') );
    if(isdigit(c))
    {
    int i=0;
    while(isdigit(c))
    {
    i=10*i+(c-'0');
    c=*s++;
    }
    printf("index:%d value:%d\n",++k,i);
    }
    else if(c)
    {
    printf("invalid character with ascii value %d found\n",c);
    return;
    }
    }while(c);
    }

    int main(void)
    {
    writenum(" 1 2 3 344 1 44");
    return 0;
    }


    when I copy paste from my editor why does all the formatting get lost ?

  8. #8
    Unregistered
    Guest
    what about 'atoi'

    it changes a string into an integer value. If it does not convert to a integer, it returns zero.

  9. #9
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Salem
    > when I copy paste from my editor why does all the formatting get lost ?
    Because you're not using the code tags - those magic words between square brackets.

    http://www.cprogramming.com/cboard/m...bbcode#buttons

    &#91;code]
    // paste code here
    &#91;/code]
    thanks a lot .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21