Count no of uppercase lowercase alphabets, digits & other sysmbols

I am trying to fix this program. It should count from the text entered how many alphabets in uppercase and lowercase, digits, blank spaces and 'other symbols'.

This is the program below. Please correct the mistakes please..!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
int nu,nl,nd,nb,ns;
char text[100];
nu=nl=nd=nb=ns=0;
cout<<"enter a line of text\n";
cin>>text;
for(int i=0;text[i]!='\0';i++)
{
if(isupper(text[i]))
nu++;
else if(islower(text[i]))
nl++;
else if(isdigit(text[i]))
nd++;
else if(text[i]==' '))
nb++;
else
ns++;
cout<<"total number of uppercase alphabets="<< nu << ".\n";
cout<<"total number of lowercase alphabets="<< nl << ".\n";
cout<<"total number of digits="<< nd << ".\n";
cout<<"total number of blank spaces="<< nb << ".\n";
cout<<"total number of other symbols="<< ns << ".\n";
getch();
}
Last edited on
Did you try compiling it? Your compiler should give you the errors so you can fix them.
Yes compiled in turboC++.

at line 21: Expression syntax
line 31: compound statement missing }
Last edited on
Turbo C++ is very old; you should probably get a newer IDE/compiler.

You should be using int main().

If you indent your code the error should become obvious.
-first error you didn't use using namespace std; or using std::cout and using std::cout or std::cin/std::cout
-second error:else if instead of elseif
-third error:line 21:
elseif(text[i]==' '))
you have to write:else if(text[i]==' ')) //only one space between ' '
also where is the for loop ending:there should be a } on line 25
Last edited on
I am using turbo C++ and my task is to run in it only not the latest ones.
so would i be needing namespace and all

already did the else if and one space and put an extra } too.
now only 1 errors and 1 warning.

Line 21, Error: Expression syntax
Warning last line: Expression should return a value

Added int main()
and return 0;
Last edited on
You can't have more than one character inside a ' '. You have three spaces inside that, which is illegal.
Yes I changed that..
Trying this way in Codeblocks but it's returning

"counting - Debug" uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping...
Nothing to be done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>

using namespace std;

int main()
{
clrscr();
int nu,nl,nd,nb,ns;
char text[100];
nu=nl=nd=nb=ns=0;
cout<<"Enter a line of text.\n";
gets(text)
for(int i=0;text[i]!='\0';i++)
{
if(isupper(text[i]))
nu++;
else if(islower(text[i]))
nl++;
else if(isdigit(text[i]))
nd++;
else if(text[i]==' '))
nb++;
else
ns++;
cout<<"total number of uppercase alphabets="<<nu<< ".\n";
cout<<"total number of lowercase alphabets="<<nl<< ".\n";
cout<<"total number of digits="<<nd<< ".\n";
cout<<"total number of blank spaces="<<nb<< ".\n";
cout<<"total number of other symbols="<<ns<< ".\n";
getch();
}}
this is another case in Codeblocks..!!

what about in Turbo C++, I am getting only 1error, why is that...
the Expression Syntax for
else if(text[i]==' '))
there is 2 closing parethesis:
it should be else if(text[i]==' ')
ohhh.. didnt see that...thankss...
now no errors....just the warning that
Function should return a value
with void main
and deleting the return 0;
eliminated the warning too....
now compiled and runs..!!!

but I am not getting correct answers....its just giving lowercase alphabets 1 no matter whatever i types....
and when i press enter it doesnt close the window but again gives out repeadtedly.
I tried this on my compiler Microsoft VS2010
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>

using namespace std;

int main()
{
//no clrscr() because it isn't present in Microsoft VS2010(at least at my knowledge) (and no need to use it here for it)
int nu,nl,nd,nb,ns;
char text[100];
	nu=nl=nd=nb=ns=0;
	cout<<"Enter a line of text.\n";
	gets(text);
	for(int i=0;text[i]!='\0';i++)
	{
		if(isupper(text[i]))
			nu++;
		else if(islower(text[i]))
			nl++;
		else if(isdigit(text[i]))
			nd++;
		else if(text[i]==' ')   //corrected here
			nb++;
		else
			ns++;
	}   //and here
	cout<<"total number of uppercase alphabets="<<nu<< ".\n";
	cout<<"total number of lowercase alphabets="<<nl<< ".\n";
	cout<<"total number of digits="<<nd<< ".\n";
	cout<<"total number of blank spaces="<<nb<< ".\n";
	cout<<"total number of other symbols="<<ns<< ".\n";
	getch();
}//and here 

I got the right answers
Yes, yes..Correct answers now..
Thanks man...Great help..!!
I am able to run in turboC++ as well
Topic archived. No new replies allowed.