Write a complete C++ program which ignores any lower case (A to Z and a to z) en
ID: 3546803 • Letter: W
Question
Write a complete C++ program which ignores any lower case (A to Z and a to z) entered by the user from the keyboard and counts the number of characters that are not letters. Your program should output to the file letters.out the upper case letters entered by the user and their count. Your program should stop after three letters are read. Your program should work for any character entered not just the one entered in the example below. Assume the user enters only letters, and that each letter is seperated by a space.
Example:
If the user enters a # 3 $ ! B / * F
The file letters1.out will contain # 3 $ ! / * 6
where 6 represents the number of upper case letters entered by the user.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream myFile;
myFile.open("a.out");
string x;
getline(cin, x);
int i, count = 0, l = 0;
for(i = 0; i < x.size(); i++){
if((x[i] >= 97 && x[i] <= 122) || (x[i] >= 65 && x[i] <= 90)){
l++;
if(l == 3) break;
continue;
}
else{
if(x[i] != ' '){
myFile << x[i] << " ";
count++;
}
}
}
myFile << count;
myFile.close();
cout << "The result has been saved in file a.out in the same directory of the program ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.