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
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();
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();
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
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.
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.
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.