Reputation: 71
Im having trouble with my programm that should count every symbol in a string that the user sent to the stream. It counts consonants, vowels, 'ff', 'fl', 'fi' and '\t', '\n' and ' '. The problem is that the programm works pretty good, but still doesnt count spaces, tabs and new lines. I've tried to use noskipws manipulator but it doesnt read anything untill the first word.
Here it is:
int aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0,
consonantCnt=0;
int ffCnt = 0, flCnt = 0, fiCnt = 0;
int spaceCnt = 0, newlCnt = 0, tabCnt = 0;
char *check = new char[100];
while ( cin >> check ) {
for ( int ix = 0; ix < strlen(check); ++ix ) {
switch ( check[ix] ) {
case 'a': case 'A':
++aCnt;
break;
case 'e': case 'E':
++eCnt;
break;
case 'i': case 'I':
++iCnt;
break;
case 'o': case 'O':
++oCnt;
break;
case 'u': case 'U':
++uCnt;
break;
case ' ':
++spaceCnt;
break;
case '\t':
++tabCnt;
break;
case '\n':
++newlCnt;
break;
default:
if ( isalpha( check[ix] ) )
++consonantCnt;
break;
}
if ( check[ix] == 'f' ) {
++ix;
switch ( check[ix] ) {
case 'f':
++consonantCnt;
++ffCnt;
break;
case 'i':
++fiCnt;
++iCnt;
break;
case 'l':
++consonantCnt;
++flCnt;
break;
case 'I':
++iCnt;
break;
case 'a': case 'A':
++aCnt;
break;
case 'e': case 'E':
++eCnt;
break;
case 'o': case 'O':
++oCnt;
break;
case 'u': case 'U':
++uCnt;
break;
case ' ':
++spaceCnt;
break;
case '\t':
++tabCnt;
break;
case '\n':
++newlCnt;
break;
default:
if ( isalpha( check[ix] ) )
++consonantCnt;
break;
}
}
}
}
delete [] check;
cout << "Встретилась a: \t" << aCnt << '\n'
<< "Встретилась e: \t" << eCnt << '\n'
<< "Встретилась i: \t" << iCnt << '\n'
<< "Встретилась o: \t" << oCnt << '\n'
<< "Встретилась u: \t" << uCnt << '\n'
<< "Встретилось согласных: \t" << consonantCnt << '\n'
<< "Встретилось fl: \t" << flCnt << '\n'
<< "Встретилось fi: \t" << fiCnt << '\n'
<< "Встретилось ff: \t" << ffCnt << '\n'
<< "Встретилось символов табуляции: \t" << ffCnt << '\n'
<< "Встретилось символов пробела: \t" << ffCnt << '\n'
<< "Встретилось символов новой строки: \t" << ffCnt << '\n'
<< '\n';
Upvotes: 0
Views: 1285
Reputation: 1504
int tabCount = 0, spaceCount = 0, newlineCount = -1;
while(std::getline(std::cin, line)){ //getline reads in a line to a string tokenized by \d by default
newlineCount++; //every time there's a new loop, theres a new newline.
for(auto c : line) {
if(c == '\t')
tabCount++;
else if(c == ' ')
spaceCount++;
}
}
You can use this basic format to read in strings line by line from std::cin or any other input stream. std::getline will automatically tokenize by '\n' so you can start your newline count from -1 (assuming you get at least one line) and loop over your string and count any character you want.
Upvotes: 1