array of strings problem

This is a discussion on array of strings problem within the C Programming forums, part of the General Programming Boards category; i am trying to place the read line to array of strings. but it only gets the first word. example: ...

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    array of strings problem

    i am trying to place the read line to array of strings. but it only gets the first word.
    example:
    this is a line seperated by whitespaces

    therefore:
    argString[0] = this
    argString[1] = is
    ..
    ...

    Code:
    char argString[20][200];
    Code:
            for (y; y<strlen(pReadLine); y++)
            {
                    argString[x][y]=pReadLine[y];
                    if (isspace(pReadLine[y]))
                    {
                            argString[x][y]='\0';
                            printf ("ArgString: %s\n", argString[x]);
                            x++;
                    }
            }
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    You're never resetting y. So when it gets to the second word you're writing the first letter to argString[1][strlen(argString[0]) + 1] instead of argString[1][0].

    Also, you're going to have to consider what to do with the last word since it won't have a space after it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    Quote Originally Posted by itsme86
    You're never resetting y. So when it gets to the second word you're writing the first letter to argString[1][strlen(argString[0]) + 1] instead of argString[1][0].
    resetting y would cause an infinite loop therefore it will be writing the same word in argstring[x]

    y=strlen(pReadLine); // therefore resetting it will cause the loop to be infinte
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Well then you're going to have to add another index variable I guess, huh?

    Don't make the mistake of thinking that I don't know what resetting y would do.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM
  5. multidimentional array of strings ???
    By null in forum C Programming
    Replies: 2
    Last Post: 09-26-2001, 11:20 PM

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