Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Program that counts lowercase, uppercase, numbers, and other

$
0
0
I have written a program that counts the number of lowercase, uppercase, nubers, and other characters in a continuous string. The problem I am running into is when it counts through the string it returns the wrong count. I have already tried running each if and else if statement individually meaning I change the else to an if and only ran it by itself doing this they work fine as in they return the correct count. When I string them all together as below it will not do the correct count if I mix lowercase, uppercase, numbers, and other characters. I am at a lose to where to go from here.
#include <iostream>
using namespace std;

int main()
{

char s[50];
int i;
int lowercase = 0;
int uppercase = 0;
int numbers = 0;
int other = 0;
int total;

//User Prompted Input Information
cout << " Enter a contiuous string of characters with no blank spaces "<<endl;

cout << " (Example : Y0UrStr1nG%#&^) " <<endl << endl;

cout << " Enter your string: ";

cin >> s;

cout <<endl;

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

i = 0;

while (s[i] != 0)
{
if ((s[i] >= 'a' && s[i] <= 'z')){
   lowercase++; 
   i++;
}
else if ((s[i] >= 'A' && s[i] <= 'Z')){
   	uppercase++; 
	i++;
}	 
else if ((s[i] >= '0' && s[i] <= '9')){
	numbers++;
	i++;
}
else  
   other++;
   i++;
}

total = lowercase + uppercase + numbers + other;

cout << "Your string has " << lowercase << " lowercase letters." << endl;
cout << "Your string has " << uppercase << " uppercase letters." <<endl;
cout << "Your string has " << numbers << " numbers." <<endl;
cout << "Your string has " << other << " other characters." <<endl;
cout << "Your string has " << total << " total characters." <<endl;


system ("pause"); //pause for Dev-C++

return 0;
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>