Answer:
#include <iostream>
#include <string>
using namespace std;
int
stringLength (string str)
{
int i = 0;
int count = 0;
while (0 == 0)
{
if (str[i])
{
i++;
count++;
}
else
{
break;
}
}
return count;
}
int
main ()
{
string userinput;
cout << "Enter string ";
getline (cin, userinput);
int string_length = stringLength (userinput);
int vowels=0;int numeric=0;int consonants=0;int special = 0;
for (int i = 0; i < string_length; i++)
{
if (userinput[i] == 'a' ||
userinput[i] == 'e' ||
userinput[i] == 'i' ||
userinput[i] == 'o' ||
userinput[i] == 'u' ||
userinput[i] == 'y' ||
userinput[i] == 'A' ||
userinput[i] == 'E' ||
userinput[i] == 'I' ||
userinput[i] == 'O' || userinput[i] == 'U' || userinput[i] == 'Y')
{
vowels++;
}
else if (userinput[i] - 48 > 0 && userinput[i] - 48 <= 9)
{
numeric++;
}
else if (userinput[i] == '~' ||
userinput[i] == '!' ||
userinput[i] == '@' ||
userinput[i] == '#' ||
userinput[i] == '$' ||
userinput[i] == '%' ||
userinput[i] == '^' ||
userinput[i] == '&' ||
userinput[i] == '*' ||
userinput[i] == '(' ||
userinput[i] == ')' ||
userinput[i] == '_' ||
userinput[i] == '+' ||
userinput[i] == '-' ||
userinput[i] == '+' ||
userinput[i] == ':' ||
userinput[i] == ':' ||
userinput[i] == '?' ||
userinput[i] == '/' || userinput[i] == '<' || userinput[i] == '>')
{
special++;
}
else
{
consonants++;
}
}
cout << vowels << " vowels and " << consonants << " consonants and " << special
<< " special characters and " << numeric << " numbers." << endl;
return 0;
}
Step-by-step explanation:
- Take input from user using getline.
- Write a function to find string length. Iterate through string index in an infinite while loop, check if index is true increase counter and index by 1, if its false break loop and return count.
- Loop through string using for loop and terminating conditions at index<string length.
- Create variable vowel,numeric,special,consonant and initialize with 0;
- For vowels add If statement with condition string[index]=="vowel" for all vowel using OR operator (||).If true increase vowel by 1.
- For special character add If statement with condition string[index]=="character " for all characters using OR operator (||).If true increase special by 1.
- For numeric add If statement with condition string[index]-48>0&& string[index]-48<=9 .If true increase vowel by 1.
- Else increase consonant by 1.
- Print all variable.