Hello Frank5093,
Some things I noticed when I tried to compile the program.
You define variables an "int"s, but an example is:
1 2 3 4 5 6 7 8
|
string getNextWord(string sentence, int i)
{
string word;
char letter;
letter = sentence[i];
while (letter != ' ' && i < sentence.length())
| |
When you get to the while loop
sentence.length()
the ".length()" and ".size()" functions return a "size_t" type of variable. The only guarantee is that "size_t" is an "unsigned" type. The value returned could be an int or a long. This would depend on your computer and header files. Also called "implementation defined".
The compiler should produce a warning something like:
Most of the time this will not be any real problem and the program will work, but it is a potential problem. It is best to learn when to use "size_t".
Some other little things:
i = i + 1;
can be shortened to
i++;
and
word = word + letter;
and the like can be shortened to
word += letter;
.
The function "isVowel" can be shortened to:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
bool isVowel(char letter)
{
letter = tolower(letter);
if (letter == 'a' || letter == 'e' ||
letter == 'i' || letter == 'o' ||
letter == 'u')
{
return true;
}
return false;
}
| |
You do not need all the extra code if you can use what you have and the way the language works. since the if statement returns true everything else is skipped and if the if statement is false the only thing left is to return false.
And for something like:
if (isVowel(word[0]) == true)
. First understand an if statement, while loop and the middle part of a for loop will take whatever is in there and evaluate it to either a (0) zero or something other than (0) zero. This is then converted to a bool type of (0)zero for false or (1) for true. Saying (== true) or (false) is kind of redundant since it will eventuall become either (0)zero false or (1) true.
You can use this to your advantage since the function returns a bool in the first place by writing
if (isVowel(word[0]))
or
(!isVowel(word[0]))
for needing a false to be true. It saves a little time and typing.
The last thing I noticed is the test string "This is a test." produced
isThay isway away est.tay
Notice that the period did not come out where it should.
For fun you might want to work on changing the whole string to lower case then capitalize the first letter of the first transformed word.
Andy