Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The program needs to be able to print the number of lowercase letters, uppercase letters, numbers, and special characters that a string with no spaces that the user enters has. i have everything except how to do the special characters. any suggestions?

C#
#include <iostream>
#include <string> 
using namespace std ;


int main()
{
    char str[100]  ;     // character string max is 100 characters
    int i ;
    int upperCase ;
    int num ;
    int lowerCase ;
    int spec ;
    
    cout << "Please enter a continuous string of characters with no spaces" << endl ;
    cout << "(example: ASO@23iow$)" << endl << endl ;   //shows an example and then adds a blank line
    cout << "Enter your string: " ;
    cin >> str ;
    cout << endl ;
    
    
    i = 0 ;
    
    while(str[i] != 0)
    {
        if ((str[i] >= 'a') && (str[i] <= 'z'))   // "i" represents each idividual character that the computer is checking
            lowerCase++ ;                        // if "i" is within a-z the computer adds 1 to variable "lowerCase"
        if ((str[i] >= '0') && (str[i] <= '9'))
            num++ ;
        if ((str[i] >= 'A') && (str[i] <= 'Z'))
            upperCase++ ;
            i++ ;                                // tells the computer to continue going through the string while "i" is not 0
        
    }
    
    cout << "your string has " << i << " characters" << endl ;
    
    cout << "Your string has " << lowerCase << " lowercase letters." << endl ;   //prints the number of lowercase characters
    
    cout << "Your string has " << num  << " numbers in it." << endl ;       //prints the number of numbers in the string
    
    cout << "Your string has " << upperCase << " uppercase letters." << endl ;  //prints the number of uppercase characters
    
    cout << "Your string has " << spec << " special characters." << endl ;
        return 0 ;
}


What I have tried:

I have tried str[i] != lowerCase str[i] != upperCase etc etc but that just ultimately prints the number of characters in the string.
Posted
Updated 3-Sep-16 6:25am
Comments
Richard MacCutchan 3-Sep-16 12:45pm    
You should use if ... else clauses. You have most of it already, you just need to add the final else which represents special characters. Something like:

if (numbers)
{
increment number count
}
else if (lowercase)
{
increment lowercase count
}
else if (uppercase)
{
increment uppercase count
}
else
{
increment specials count
}
Member 12718557 3-Sep-16 12:48pm    
THANK YOU!

1 solution

That depends on your definition of "special character".

If that means a character that isn't covered by the ranges you already have in your code then it's pretty easy. It's just the length of the string minus all the other counts you have.
 
Share this answer
 
Comments
Member 12718557 3-Sep-16 12:31pm    
sorry im new to programming so something like " if (str[i] -- lowerCase) ?? not sure how to do what you are saying @DaveKreskowiak
Dave Kreskowiak 3-Sep-16 14:26pm    
It has nothing to do with looking at a character and comparing it to something else. You've already done that and have your other character counts, do you not?

I've already told you how to do it, but it's up to you to write and debug the code. I'm not going to do your homework for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900