Hmm, you're right. I wasn't aware of that. It's nice to know.
Also:
Code:
else if(((ch == ' ')||(ch == ',')||(ch == '?')||(ch == '!')||(ch == ':')||(ch == '"')||(ch == '.'))&&(i > 0)){
formWord[i] = '\0';
word = formWord;
if (searchArray(words,word,wordCount, searchCount)){
count[searchCount] = (count[searchCount] + 1);
}
There's no need to use the temporary variable word. You can pass on the char array formWord directly to searchArray(), which takes a string parameter.
Code:
if((ch != ' ')&&(ch != ',')&&(ch != '?')&&(ch != '!')&&(ch != ':')&&(ch != '"')&&(ch != '.')){
formWord[i] = toupper(ch);
i++;
}
else if(((ch == ' ')||(ch == ',')||(ch == '?')||(ch == '!')||(ch == ':')||(ch == '"')||(ch == '.'))&&(i > 0)){
You don't need to repeat those conditions twice. This would work.
Code:
if((ch != ' ')&&(ch != ',')&&(ch != '?')&&(ch != '!')&&(ch != ':')&&(ch != '"')&&(ch != '.')){
formWord[i] = toupper(ch);
i++;
}
else if(i > 0){
And an is_word() function like I suggested would make it even easier.
Code:
bool is_word(char ch) {
return (ch != ' ')&&(ch != ',')&&(ch != '?')&&(ch != '!')&&(ch != ':')&&(ch != '"')&&(ch != '.');
}
if(is_word(ch)){
formWord[i] = toupper(ch);
i++;
}
else if(i > 0){
You could even make use of strchr():
Code:
bool is_word(char ch) {
return !strchr(" ,?!:\".", ch);
}
Code:
for (int j = 0; j < 200; j++){
cout << words[j] << " " << count[j] << setw(10) << endl;
}
What if there weren't 200 words? I'd use wordCount or numCount, both of which seem to do exactly the same thing. Perhaps you could combine them into one variable.