I'm looking for a simple and basic way (ideal for beginners to learn the easiest way) to write a program in C++ which gets a string from the user and outputs the number of uppercase letters, lowercase letters and integers (numbers). I'm pretty amateur at using C++ syntax so please help me with an easy-to-understand syntax. Thanks!
EDIT: Here is a very simple code that I found in Google and did some changes and corrections:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char array1[50];
int i = 0, lowercase = 0, uppercase = 0, numbers = 0, total;
cout << "Enter a string: "<<endl;
cin >> array1;
cout <<endl;
while (array1[i] != 0){
if(array1[i] >= 'a' && array1[i] <= 'z'){
lowercase++;
i++;
}
else if (array1[i] >= 'A' && array1[i] <= 'Z'){
uppercase++;
i++;
}
else if (array1[i] >= '0' && array1[i] <= '9'){
numbers++;
i++;
}
else
i++;
}
total = lowercase + uppercase + numbers;
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 " << total << " total characters." <<endl;
getch();
return 0;
}
So in this code; we are assuming that the end of a string has the integer 0, right? How can we change it so we can have spaces in the string?
Try:
Save as:
Compile as:
Sample run:
Using
7
to mean any number,x
any lowercase letter, andX
any uppercase letter. So this input has ten digits, 16 lower case letters and six upper case letters.Hope this code is easy to understand: just make a string and call a function. It might not work on Windows, though.
The function call
system
is really useful, you can pass it things like"yes | cat yes"
or"rm -f *.?pp"
.