The reason you're getting a lot of junk is (partly) because you don't use a lot of the array spaces.
Since the extra spots aren't initialized, when you try to print out everything in the array, you get what you want (if you coded it correctly) plus all of the junk in the unused array slots.
So I'd suggest initializing all of the values of
words to zero first before you do anything else.
Now, as for the rest of the code:
12 13 14 15 16 17 18 19
|
if ((clean[i] >= 'A')&&(clean[i] <= 'Z'))
{
words[i][j] = clean[k];
k++;
}
else
break;
|
First, it should be
if ((clean[k] >= 'A')&&(clean[k] <= 'Z'))
.
Now, even if the condition is
false
, you still need to increment
k, or else
k will get stuck on the position of the first space.
There's also going to be a point where the string you inputted into
clean ends, but you might still have some extra array slots after that.
So, at the beginning of the
j loop, you need to check for if
clean[k] == '\0' (checking for the end of the string), and if that happens, you'll have to
break
out of both loops.
After you do all of that, you should have each word (assuming you don't have lowercase letters or other punctuation marks) split into its own
words[i] spot.
Just keep in mind when reversing:
-- Each of the
words[i]s is not necessarily going to have all of the
j spots filled (if you know what I mean). This comes simply from the fact that not every word is going to have 20 letters.
If you initialized all of the values of
words to zeros at the beginning like I mentioned above, you can be sure that each word will be either 20 letters long or will end with a null character (
'\0'
).
-- Not even all of the
words[i]s themselves are going to be filled (since there won't necessarily always be 25 words).
The unused spots will be filled with all null characters if you initialized
words at the beginning.