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.