Thread: String Problem

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    String Problem

    Hi, i have a string containing " i am a god" and i want to take this array of charecters and split it into four strings(where the spaces are) so the first string is "i" , second is "am" etc... however i am having problems doing this. i am using gcc , however my program is crashing, here is the function i have written to do this, however its not working correctly..... any help is appreciated.

    Code:
      
        
    int split(char *line)
    {   
        char splitstring [4][20];
        int charnumber = 0;
        int stringno = 0;
        int a = 0;
        while ( (  line[a] != '\n' ) ){
              if ( line[a] == ' ' ){
                  splitstring[stringno][charnumber] = '\0';
                  stringno++;
              }
              else{
                    splitstring[stringno][charnumber] = line[a];
                    a++;
                    charnumber++;
              }     
        }
    }
    line is the array of charecters "i am a god" and i'm trying to split it into an array of strings , therefore

    splitstring[0] = "i"
    splitstring[1] = "am"

    etc..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > stringno++;
    reset charnumber back to 0 as well
    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.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks salem. my while loop was also incorrect because i was checking for a newline when i should have been checking for a '\0'.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    i have modified this slightly to check that the array does not go out of bounds, however another problem i am having is that if i input " i am a god" (with more than one space between each word) i am left with strings that contain spaces e.g. splitstring[0] = " i"
    splitstring[1] = " am" when i dont want this as later on i have to use string compare on these strings. how do i modify my loop to account for this?

    Code:
     
    
    int split(char *line, int linecount,int currentline)
    {   
        char splitstring [5][20];
        int charnumber = 0;
        int stringno = 0;
        int a = 0;
        while ( line[a] &&  (  line[a] != '\0' ) ){
              if ( ( line[a] == ' ' ) ){
                  splitstring[stringno][charnumber] = '\0';
                  stringno++;
                  charnumber = 0;
                  a++;
              }
              if (stringno >= 5) break;
              else{
                    splitstring[stringno][charnumber] = line[a];
                    a++;
                    charnumber++;
                    if (charnumber >= 20){
                    break;
                    charnumber = 0;
                    a++;
                    stringno++;
                    if (stringno >= 5) break;
                }
              }
              //splitstring[stringno][charnumber] = '\0';
                  
        }
    any help is appreciated. i know that somewhere i need to check whether the next character is also a space (if ive just encountered a space) , however i am not sure where to do this.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about right where you first find out that you've found a space?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while ( line[a] && ( line[a] != '\0' ) )
    Which is the same as saying
    while ( ( line[a] != '\0' ) && ( line[a] != '\0' ) )
    Trying to make doubly sure?

    A better way would be to do this (it also removes that hacky break statement from your code as well)
    while ( stringno < 5 && line[a] != '\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.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    24
    i want to take this array of charecters and split it into four strings
    why not use strtok?

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    i have encountered a new problem with the code i posted above, if there is no space after the last string then the last string is not terminated properly , e.g "i eat yellow people" ends up with people not being terminated properly, how can i fix this problem? any help is appreciated!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how can i fix this problem?
    You store a \0 in the correct place

    Seriously, this is up to you to fix.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    i fixed my problem .thanks .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM