Help strings :(

Write a program which prompts user to enter a string of text. Once entered, you need to present a
summary of the text in the following manner.
Total number of vowels in the text.
Total number of spaces in the text.
Total number of upper case characters in the text.
This is what i did so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

	int i=0;
	int upper=0;
	int space=0;
	string s("Enter a string ");
	int b = s.length();
	int l=0;
for(int i=0 ; i<=b;++i)
{
if (isspace(s.length())==true)
{
	++space;
}
if (isupper(s.length())==true)
{
	++upper;
}
}
cout<<"Total number of spaces : "<<space<<endl;
cout<<"Total number of upper : "<<upper<<endl;

getch();

output is always zero why is that i tried !
Well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	int i=0;
	int upper=0;
	int space=0;
	string s("Enter a string ");
	int b = s.length();
	int l=0;
for(int i=0 ; i<b;++i) // Note: no =
{
if (isspace(s.length() s[i])==true)
{
	++space;
}
if (isupper(s.length() s[i])==true)
{
	++upper;
}
}
cout<<"Total number of spaces : "<<space<<endl;
cout<<"Total number of upper : "<<upper<<endl;

getch();
Last edited on
yes it shows output but it gives wrong result now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int upper = 0;
    int space = 0;
    string s("Enter a string ");
    int b = s.length();

    for (int i=0; i<b; ++i) 
    {
        if (isspace(s[i]))
            ++space;

        if (isupper(s[i]))
            ++upper;
    }

    cout << "Total number of spaces : " << space << endl;
    cout << "Total number of upper :  " << upper << endl;

Output:
Total number of spaces : 3
Total number of upper :  1
Last edited on
YEs it works but in line 8 i wrote if (ispace(s[i])==true) but it doesn't work that way why shouldn't sispace return a bool type it's ok if you don't want to reply you helped me alot anyway thanks cheers :)
ispace(s[i])==true
doesn't work, because true simply means "any non-zero value".

When the character is a space, the function presumably returns a non-zero value, but we don't know for certain what that value is, so testing for it being exactly equal to some other non-zero value may not give the desired result.

Try this:
1
2
    cout << int(isspace(' ')) << endl;
    cout << int(true) << endl;

My compiler gives this output:
8
1
My one gives the same output but i cant make sense out of it what is 8 and 1
8 is a non-zero value, therefore it is considered true
1 is a non-zero value, therefore it is considered true
So, although both values correctly represent true, it doesn't make sense to test whether 8 is equal to 1.

http://www.cplusplus.com/reference/cctype/
an int value of 0 means false, and an int value different from 0 represents true.
Last edited on
yes but our compiler gives same output would it give same output on every computer shouldn't it give a random number like you said though
There's no way to guarantee the specific value it will give, The only thing which is certain is that which is stated in the specification given on the reference page,
"an int value of 0 means false, and an int value different from 0 represents true".

The point being, you simply test the condition, as you would any other, for example if you want to know whether x is greater than y,
you put this: if (x > y)
rather than if ((x > y) == true)

Indeed, if it was necessary to test whether the condition was equal to true, you would never stop...
if ((((((x > y) == true) == true) == true) == true) == true) // .... etc.
Last edited on
I am going through reference first thing tommorow thank you so much
Topic archived. No new replies allowed.