I've tried submitting this code so many times, until I changed the input from string to char array, and it got AC.
Here is my code which got WA (it's exactly the same output of the code I got AC, the only difference is input by char array instead of string)...
Code: Select all
#include <iostream>
#include <string>
using namespace std;
int wordCounter(string str)
{
bool inWord = false; // flag to check whether the current position is in a word (contains alphabets)
int wordCount = 0;
for (int i = 0; str[i] != '\0' ; i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
if (!inWord)
{
inWord = true;
wordCount++;
}
}
else
{
inWord = false;
}
}
return wordCount;
}
int main()
{
string str;
while(getline(cin, str))
{
cout << wordCounter(str) << "\n";
}
}