Question

In: Computer Science

I need a C++ program using while loops that counts the number of characters in a...

I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have the main while loop that counts the total number of characters in a sentence, but can't get the program to count the individual vowels and the consonants. Could you comment the steps so I can figure out what I'm missing?

Solutions

Expert Solution

I WROTE THE CODE ALONG WITH THE COMMENTS:

CODE:

#include <iostream>

using namespace std;

int main()
{
//variables declaration.
char sentence[1000],ch;
int i=0,count_char=0,count_a=0,count_e=0,count_i=0,count_o=0,count_u=0,count_consonants=0;
  
cout<<"Enter sentence: ";
while(ch!='!' && ch!='.') //condition checking.
{
cin>>ch; //scan one by one character.
sentence[i]=ch;
i++;
  
if((ch>='a'&& ch<='z') || (ch>='A' && ch<='Z')) //condition for character
{
if(ch=='a' || ch=='A') //condition for a and A.
count_a++;
else if(ch=='e' || ch=='E') //condition for e or E.
count_e++;
else if(ch=='i' || ch=='I') //condition for i or I.
count_i++;
else if(ch=='o' || ch=='O') //condition for o or O.
count_o++;
else if(ch=='u' || ch=='U') //condition for u or U.
count_u++;
else //condition for consonants.
count_consonants++;
count_char++;
}

  
}
  
//display results.
cout<<"Number of characters: "<<count_char<<endl;
cout<<"Number of a's: "<<count_a<<endl;
cout<<"Number of e's: "<<count_e<<endl;
cout<<"Number of i's: "<<count_i<<endl;
cout<<"Number of o's: "<<count_o<<endl;
cout<<"Number of u's: "<<count_u<<endl;
cout<<"Number of consonants: "<<count_consonants<<endl;
  

return 0;
}

OUTPUT:

SCREENSHOT OF THE CODE:


Related Solutions

Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Write a C program that counts the number of non white-space characters in an inputtext file....
Write a C program that counts the number of non white-space characters in an inputtext file. Program takes as command argument(s)the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writesthe output to an output file. The command format is as follows: command -f inputfile outputfile or, command -s inputfile -f indicates writing to an output file; -s indicates displaying the output on...
Write a program in c++ using only while and for loops . Use of arrays and...
Write a program in c++ using only while and for loops . Use of arrays and functions is not allowed. Given the first value, generate the next ten terms of the sequence like 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, … Explaination with code is required.
Write a series of codes using WHILE loops C++ 1. Ask the user for a number...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number and adds even numbers for 1 to the user entered number. 2. Write another piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 3. Write another piece of code that asks user for a file name and then add up all the numbers for the file.
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Q3)   Write a C program that counts the number of odd numbers with using function count()...
Q3)   Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT