Bellow code is only counting other infact it count many characters (upper,lower,digits) as others.i want a simple beginner program which count lower,upper,digits,space,punc etc..
#include <fstream>
#include <iostream>
#include <conio.h>
#include <ctype.h>
usingnamespace std;
int main ()
{
char c;
int lc=0;
int uc=0;
int dc=0;
int wc=0;
int pc=0;
int ot=0;
cout<<"Please Enter a character string : ";
cin>>c;
while(c=getchar()!='\n')
{
if(islower(c))
lc++;
elseif(isupper(c))
uc++;
elseif(isdigit(c))
dc++;
elseif(isspace(c))
wc++;
elseif(ispunct(c))
pc++;
else ot++;}
cout<<"\nTotal Number of lower case are :"<<lc;
cout<<"\nTotal Number of Upper case are :"<<uc;
cout<<"\nTotal Number of digits are :"<<dc;
cout<<"\nTotal Number of spaces are :"<<wc;
cout<<"\nTotal Number of punctuations are :"<<pc;
cout<<"\nTotal Number of other character are :"<<ot;
return 0;
}
i figured it out there was not parenthesis around c=getchar() it was like(c=getchar()).but now this code is not count first character if i type SS it counting 1 uper case if i ss it counting 1 lower case ..if i type ssSS it count 1 lower and 2 uper case.if i type SSss it counting 1uper and 2 lower case..
// #include <fstream> You dont need this
#include <iostream>
//#include <conio.h>
#include <cctype> //#include <ctype.h>
#include <string>
usingnamespace std;
int main ()
{
//use descriptive naming
int lowerCount = 0;
int upperCount = 0;
int digitCount = 0;
int wspaceCount = 0;
int puntCount = 0;
int othersCount = 0;
string str;
cout<<"Please Enter a character string : ";
//use getline() for strings
getline(cin, str); // cin>>c;
for(unsignedint x = 0; x < str.size(); ++x)
{
char c = str[x];
if(islower(c)) ++lowerCount;
elseif(isupper(c)) ++upperCount;
elseif(isspace(c)) ++wspaceCount;
elseif (isdigit(c)) ++digitCount;
elseif(ispunct(c)) ++puntCount;
else ++othersCount;
}
cout <<"Total Number of lower case are: " << lowerCount << endl;
cout <<"Total Number of Upper case are: " << upperCount << endl;
cout <<"Total Number of digits are: " << digitCount << endl;
cout <<"Total Number of spaces are: " << wspaceCount << endl;
cout <<"Total Number of punctuations are: "<< puntCount << endl;
cout <<"Total Number of other character are: " << othersCount << endl;;
return 0;
}
Both of them read the next character from the standard input stream and store the value in c. It is the first read operation in your program so the next character is also the first.
cin and get are both used to read from stream. so it is not an error for cin to read the first character. the program just worked the way you sequenced the instructions. Line 17, takes 1 character away form the string