counting characters in a string

Jun 18, 2010 at 11:51pm
Hello,
I am new to c++. I have been trying to write a program that counts the total number of characters in a string. The program then outputs the total of letters, total of numerical characters, and total of other characters. The program will not count numbers or other characters but it will count the letters fine. Any help would be greatly appreciated. This is the source code:


#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
char s[50];
int i;
int letters = 0; // letters
int numbers = 0; // numerical characters
int other = 0; // other characters
int total; // total of all characters

cout << "Enter a continuous string of characters with no blank spaces" << endl;
cout << "(example: aBc1234!@#$%)" << endl;
cout << endl;
cout << "Enter your string:";
cin >> s;
cout << endl;

//loop through the string, counting numbers, letters & others

////////////////////////////////////////////////////////////////////
//count letters
////////////////////////////////////////////////////////////////////

i = 0;
while (s[i] !=0)

if (( s[i] >= 'a' && s[i] <= 'z') || (s[i] <= 'A' && s[i] >= 'Z'))
{
letters++;
i++;
}

////////////////////////////////////////////////////////////////////
//count other characters
////////////////////////////////////////////////////////////////////

else
if (( s[i] >= '!' && s[i] <= ')'))
{
other++;
}

////////////////////////////////////////////////////////////////////
//count numerical characters
////////////////////////////////////////////////////////////////////

else
if (( s[i] >= '0' && s[i] <= '9'))
{
numbers++;
}

////////////////////////////////////////////////////////////////////
//calculate total of characters
////////////////////////////////////////////////////////////////////

total = letters + numbers + other;

////////////////////////////////////////////////////////////////////
//output the results
////////////////////////////////////////////////////////////////////

cout << "Your string has " << total << " total characters" << endl;
cout << letters << " letters " << endl;
cout << numbers << " numerical characters " << endl;
cout << other << " other characters " << endl;


return 0;
}
Jun 19, 2010 at 12:43am
One thing is that you use the >> operator for input. Doing so, you are limited to stop your input when the first space character (space, tab, newline etc...) is encountered. Use cin.getline(s,50,'\n'); instead.

Another thing is that you only increase i when you encounter a letter, while it should be increased every time. Also, in the way you do it you don't take care of every possible character. I suggest something like this:

1
2
3
4
5
6
7
8
9
10
while (s[i]!=0)
{
	if (/*is letter*/) letters++;

	else if (/*is number*/) numbers++;

	else other++; //anything else than letters and numbers

	i++; //don't forget this!
}

Finally, don't forget the c-library functions isalpha, isdigit, etc..., found in <cctype>:

http://cplusplus.com/reference/clibrary/cctype/
Jun 19, 2010 at 11:53am
you should use

while(s[i]!='\0')
{
if(/*is letter*/)
{letters++;i++;}

else if (/*is number*/)
{numbers++;i++;}

else
{other++;i++;} //anything else than letters and numbers

Jun 19, 2010 at 3:48pm
Or you can just use an std::string and use string::length().
http://cplusplus.com/reference/string/string/

-Albatross
Jun 19, 2010 at 6:15pm
Albatross wrote:
Or you can just use an std::string and use string::length().

Don't just read the title and post... Bear with reading a bit more to understand what he actually wants...
Jun 19, 2010 at 6:28pm
Okay... oops. I didn't see that bit about which characters are to be counted.

Still, I'd recommend using an std::string() instead of a char[50].

-Albatross
Last edited on Jun 19, 2010 at 6:30pm
Jun 20, 2010 at 5:22am
Thanks for all the help.
Topic archived. No new replies allowed.