I'm writing a simple program that puts a string in some order like:
The string is "Once upon a time"
My program has to put each word in a new line when it reads a space:
Once
upon
a
time
But, i can only get 'On' and nothing else
Here:
Code:
int whichWord = 0;
int wordIndex = 0;
int sentenceIndex = 0;
char sentence[MAX_STR_LEN];
char WORDS[MAX_WORDS][MAX_STR_LEN];
char word[] = "Once upon a time ";
int counter;
strcpy(sentence, word);
cout << sentence << endl;
while( sentence[sentenceIndex] != NULL_CHAR )
{
if (sentence[sentenceIndex] != SPACE )
{
WORDS[whichWord][wordIndex] = sentence[sentenceIndex];
wordIndex++;
}
else
{
WORDS[whichWord][wordIndex] = NULL_CHAR;
wordIndex++;
wordIndex = 0;
}
sentenceIndex++;
}
WORDS[whichWord][wordIndex] = NULL_CHAR;
for( counter = 0; counter <= whichWord; counter++)
{
cout << WORDS[counter] << endl;
}