147k views
1 vote
Hardcode the algorithms using if/else and loops not library calls. Write a program that allows the user to input a string and prints the number of vowels, consonants, numerical values 0-9, special symbols in the word. For this exercise assume ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and ‘y’ are vowels. Make sure to include capitol vowels. Special symbols → ~!@#$%^&*()_-=+?/" :;<> Please write a short paragraph, bullet points, or psuedocode describing your algorithm for solving the problem. For user input "Harry", the program should print 2 vowels and 3 consonants. For user input "Harry", the program should print "9&&qT" 2 consonants 2 Special Characters 1 numeric value]

1 Answer

5 votes

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:

  • Create a string.
  • 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.
User Thisisdog
by
8.2k points