// countLetters.cpp
// Counts the number of letters in a string.
// Uses a while loop.
#include <iostream>
using namespace std;
int main()
{
const int LENGTH = 15;
// Announce purpose of program:
cout << "Enter a string without white space, up to "
<< LENGTH << " characters.";
cout << "This program will tell you how many letters "
<< "the string contains." << endl;
// Ask user for a string, and echo it:
cout << "Enter your string:>";
char text[LENGTH + 1];
cin >> text;
cout << "You typed: " << text << endl;
// No letters have been found YET:
int countLetters = 0;
// Count letters:
int index = 0; // Start at the beginning of the string.
while ( text[index] != '\0') // until end of string
{
// If the character at index is a letter, add one
// to count of letters. Then, whether or not the
// character is a letter, prepare to look at the
// next character.
if ( (text[index] >= 'A' && text[index] <= 'Z')
|| (text[index] >= 'a' && text[index] <= 'z') )
countLetters = countLetters + 1;
index = index + 1;
} // while
// Output result:
cout << "Your string contains " << countLetters
<< " letters." << endl;
return 0;
} // function main