Question

Please do it with C++. Thanks:) Write a program that reads a string and a character...

Please do it with C++. Thanks:)

Write a program that reads a string and a character from the keyboard. The program should output how many times the character appears in the string. Make the program run in a loop until the string finish is input.

Sample run:

Input a string: alicia
Input a letter: a
The letter appears 2 times
Input a string: felix
Input a letter: x
The letter appears 1 time
Input a string: finish
Good Bye.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
int main() {
string str;
char ch;
int count = 0;
  
do{
count = 0;
cout<<"Input a string: ";
cin>>str;
if(str=="finish"){
cout<<"Good Bye.";
break;
}
cout<<"Input a letter: ";
cin>>ch;
for(int i=0 ;i< str.size();++i){
  
if(str.at(i)==ch)
++count;
  
}
cout<<"The letter appears "<<count<< " times"<<endl;
  
}while(1);
}



===================================================
See Output

Files saved share gcc version 4.6.3 Input a string alicia Input a letter: a The letter appears 2 times Input a string: fekix

Thanks, PLEASE RATE. let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Please do it with C++. Thanks:) Write a program that reads a string and a character...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT