Hey I am inputting this string into a this function " A toyota's a toyota". The code should count the frequency of the word and letter count and display it. The issue I am having is that is does not count them all, it misses the last word counts. Here is the snippit that I am working with.
void WordCharacter (string &input, int number) //function to see how many characters are in the words of a string and also the frequency.
{
if (number == 1)
{
cout<<"\t ----------------------------------------------------"<<endl;
cout<<"\t | Here is the results of the strings |"<<endl;
cout<<"\t ----------------------------------------------------"<<endl<<endl<<endl;
cout<<"Here is your string: "<<input<<endl;
}
if (number == 2)
{
cout<<"Here is your string: "<<input<<endl;
}
if (number == 3)
{
cout<<"Here is your string: "<<input<<endl<<endl;
}
int word1 =0, word2 = 0, word3 = 0, word4 = 0, word5 = 0;
int word6 =0, word7 = 0, word8 = 0, word9 = 0, word10 = 0;
char endline = '\n';
string temp = input;
int punct = 0;
int length = temp.length();
for (int x = 0; x < length; x++)
{
if (ispunct(temp.at(x)))
punct++;
}
int x1 = 0;
for (int x = 0; x < length; x++)
{
if (!ispunct(temp.at(x)))
temp.at(x1++) = temp.at(x);
}
int lengthNew = length - punct;
temp.resize(lengthNew);
int Count = 0;
for (int x = 0; x < lengthNew; x++)
{
if (isalpha(temp.at(x)))
{
Count++;
}
else
if (Count > 0)
switch (Count)
{
case 1:
word1++;
Count = 0;
break;
case 2:
word2++;
Count = 0;
break;
case 3:
word3++;
Count = 0;
break;
case 4:
word4++;
Count = 0;
break;
case 5:
word5++;
Count = 0;
break;
case 6:
word6++;
Count = 0;
break;
case 7:
word7++;
Count = 0;
break;
case 8:
word8++;
Count = 0;
break;
case 9:
word9++;
Count = 0;
break;
case 10:
word10++;
Count = 0;
break;
}
}
if (word1 > 0)
cout<<endl<<"There are "<<word1<<" 1 letter words in your string.\n"<<endl;
if (word2 > 0)
cout<<"There are "<<word2<<" 2 letter words in your string.\n"<<endl;
if (word3 > 0)
cout<<"There are "<<word3<<" 3 letter words in your string.\n"<<endl;
if (word4 > 0)
cout<<"There are "<<word4<<" 4 letter words in your string.\n"<<endl;
if (word5 > 0)
cout<<"There are "<<word5<<" 5 letter words in your string.\n"<<endl;
if (word6 > 0)
cout<<"There are "<<word6<<" 6 letter words in your string.\n"<<endl;
if (word7 > 0)
cout<<"There are "<<word7<<" 7 letter words in your string.\n"<<endl;
if (word8 > 0)
cout<<"There are "<<word8<<" 8 letter words in your string.\n"<<endl;
if (word9 > 0)
cout<<"There are "<<word9<<" 9 letter words in your string.\n"<<endl;
if (word10 > 0)
cout<<"There are "<<word10<<" 10 or more letter words in your string.\n"<<endl;
if (number == 3)
return;
}