Number of Letters
Lesson 4 of 5 in Coddy's Simple Character Counter using C++ course.
In C++, you can check if a character is a letter using:
bool b = std::isalpha(c)b will hold true if c is a letter, otherwise false.
Or, in short, using namespace:
bool b = isalpha(c)Challenge
EasyAdd to your existing program, count the number of letters at the string, and output it at the end like this (with a new line):
Number of letters: XXDon't delete the previous output, output it after your current outputs.
Check the test cases for examples!
Try it yourself
#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
cout << "Enter text:" << endl;
getline(cin, text);
int total = 0;
for (char c : text) {
if (c == ' ') continue;
total ++;
}
cout << "Total characters: " << total << endl;
return 0;
}