As a new student to the C language I was originally given the assignment to write a code that would count the amount of syllables in a given string. Later it was changed on me to be able to count multiple strings.Now keep in mind I'm not to far along in the class and honestly I have my concerns about whether or not I'm actually learning what I need to pass this class. So I went back and started the frustrating process of changing my code when it already worked for a different function. I managed to produce the desired format of:
Word Syllable
Harry 2 Hairy 2 Hare 2 The 2
As you can tell it's not correct as it counts the syllables of only the first word and then applies it to the others. I tried changing it to a for loop but it didn't work so I went to a while loop and I got a somewhat better result:
Word Syllable
Harry 2
Word Syllable
Hare 1
So now it correctly counts the syllables but only of every other word instead of all and double prints the table header. Now even my cout command tells me it's ambiguous even though it still runs so I'm extra confused. I'm thinking I might have to change it into an array but at this point I'm completely stumped.
Here is my code so far:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
cout << "Please enter four words: ";
string word;
while (cin >> word);
{
cin >> word;
bool last_vowel = false;
bool last_cons = false;
bool curr_vowel = false;
bool curr_cons = false;
int syllable_count = 0;
for (int i = 0; i < word.length(); i )
{
string letter = word.substr(i, 1);
if (letter == "a" || letter == "e" ||
letter == "i" || letter == "o" ||
letter == "u" || letter == "y" ||
letter == "A" || letter == "E" ||
letter == "I" || letter == "O" ||
letter == "U" || letter == "Y")
{
curr_vowel = true;
curr_cons = false;
}
else
{
curr_vowel = false;
curr_cons = true;
}
// Increment the syllable count any time we
// transition from a vowel to a consonant
if (curr_cons && last_vowel)
{
syllable_count ;
}
last_vowel = curr_vowel;
last_cons = curr_cons;
}
// Check the last letter in word.
string last = word.substr(word.length() - 1, 1);
// Add one for an ending vowel that is not an "e"
if (last == "a" || last == "i" || last == "o" ||
last == "u" || last == "y" || last == "A" ||
last == "I" || last == "O" || last == "U" ||
last == "Y")
{
syllable_count ;
}
// There has to be at least one syllable
if (syllable_count == 0)
{
syllable_count = 1;
}
cout << left;
cout << setw(10) << "Word" << setw(20) << "Syllables" << endl;
cout << "__________________________" << endl;
cout << left;
cout << setw(19) << word << syllable_count << endl;
}
return 0;
}
CodePudding user response:
Tony answer to your problem is very complicated. I saw someone writing this looks like an unbelievably difficult problem in linguistics
while researching the conditions of syllables.
I was not able to find any hard-set rule that can tell you how many syllables are there in the word.
I found some conditions on the program and checked the result to the code on this online tool https://syllablecounter.net/count my guess is they have some list of words to exclude from counting or to be included even if they do not fall under basic conditions.
Wrote the below code to count the syllables in each word of the given phrase. The program will continue to ask for phrase, Till you enter something other then y when asked to continue.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
while (true) {
cout << "Enter the string you want to count syllable for: ";
string phrase, word;
string vowels = "aeiou";
getline(cin, phrase);
//cout << phrase << endl;
stringstream X(phrase); // Object of stringstream that references the phrase string
// iterate on the words of phrase
while (getline(X, word, ' ')) {
// logic to find syllable in the word.
// Traverse the string
int syllable_count = 0;
// if word[0] in vowels count as syllable
if(vowels.find(tolower(word[0])) != std::string::npos)
syllable_count =1;
// if word[index] in vowels and word[index - 1] not in vowels count as syllable
for (int i = 1; i < word.size(); i ) {
if(vowels.find(tolower(word[i])) != std::string::npos && vowels.find(tolower(word[i-1])) == std::string::npos)
syllable_count =1;
}
// if word ends with 'e' it does not cout as syllable
if(tolower(word[word.size()-1]) == 'e')
syllable_count -=1;
// if word ends with 'le' and not of 2 char only and last 3rd word not in vowels it count as syllable
if((word.size() > 2) && (tolower(word[word.size()-1]) == 'e' && tolower(word[word.size()-2]) == 'l' ) && vowels.find(tolower(word[word.size()-3])) == std::string::npos)
syllable_count =1;
// if word end with 'y' treet it as syllable
if(tolower(word[word.size()-1]) == 'y'){
syllable_count =1;
// if word end with 'y' and have vowel in last second position count both char as one syllable.
if((word.size() > 2) && vowels.find(tolower(word[word.size()-2])) != std::string::npos)
syllable_count -=1;
}
if(syllable_count == 0)
syllable_count = 1;
cout << word << " : " << syllable_count << endl; // print word and syllable cout
}
cout << "Press y if you want to continue: ";
string stop;
getline(cin, stop);
if( stop != "y")
return 0;
}
return 0;
}
Sample result
Enter the string you want to count syllable for: troy
troy : 1
Press y if you want to continue: y
Enter the string you want to count syllable for: test
test : 1
Press y if you want to continue: y
Enter the string you want to count syllable for: hi how are you
hi : 1
how : 1
are : 1
you : 1
Press y if you want to continue: y
Enter the string you want to count syllable for: Harry hairy hare the
Harry : 2
hairy : 2
hare : 1
the : 1
Press y if you want to continue: n
You might need to add some conditions as you find certain cases. This problem is not something that can be worked with if else only.
Hope this helps.