Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. int countWords(string);
  8. int countConsonants(string);
  9.  
  10. int main() {
  11.  
  12.     char selection = 0;
  13.     string input;
  14.     char ch = 'a';
  15.     input = ch;
  16.  
  17.     cout << "Please enter a word, a sentence, or a string of numbers.\n" << endl;
  18.     getline(cin, input);
  19.  
  20.     do {
  21.  
  22.         cout << "USE THIS MENU TO MANIPULATE YOUR STRING\n"
  23.             << "---------------------------------------\n"
  24.             << "1) Inverse String\n"
  25.             << "2) Reverse String\n"
  26.             << "3) To Uppercase\n"
  27.             << "4) Count Number Words\n"
  28.             << "5) Count Consonants\n"
  29.             << "6) Enter a Different String\n"
  30.             << "7) Print the string\n"
  31.             << "Q) Quit\n" << endl;
  32.  
  33.         cin >> selection;
  34.         cin.ignore();
  35.  
  36.         switch (selection) {
  37.  
  38.         case 49:
  39.             for (int i = 0; i <= input.length(); ++i) {
  40.                 if (isupper(input[i])) {
  41.                     input[i] = tolower(input[i]);
  42.                 }
  43.                 else if (islower(input[i])) {
  44.                     input[i] = toupper(input[i]);
  45.                 }
  46.             }      
  47.             break;
  48.         case 50:   
  49.             for (int j = input.length() - 1; j >= 0; --j) {    
  50.                
  51.             }
  52.             break;
  53.         case 51:
  54.             for (int o = 0; o <= input.length(); ++o) {
  55.                 if (islower(input[o])) {
  56.                     input[o] = toupper(input[o]);
  57.                 }
  58.             }
  59.             break;
  60.         case 52:
  61.             countWords(input);
  62.             break;
  63.         case 53:
  64.             countConsonants(input);
  65.             break;
  66.         case 54:
  67.             cout << "Enter a different string : \n";
  68.             getline(cin, input);
  69.             break;
  70.         case 55:   
  71.             cout << input << endl;
  72.             break;
  73.         case 'q':
  74.             break;
  75.         case 'Q':
  76.             break;
  77.         default:
  78.             cout << "Invalid selection. Please try again." << endl;
  79.         }
  80.     } while (selection != 'Q' && selection != 'q');
  81.    
  82.     cout << "You have chosen to quit the program. Thank you!" << endl;
  83. }
  84. int countWords(string input) {
  85.  
  86.     int sum = 1;
  87.  
  88.     for (int i = 0; i <= input.length(); ++i) {
  89.         if (isspace(input[i])) {
  90.             sum++;
  91.         }
  92.     }
  93.     cout << "The string " << "\"" << input << "\"" << " has " << sum << " word(s) " << "\n\n";
  94.     return sum;
  95.    
  96. }
  97. int countConsonants(string input) {
  98.  
  99.     int sum = 0;
  100.  
  101.     for (int e = 0; e <= input.length(); ++e) {
  102.  
  103.         if (input[e] != 'a' && input[e] != 'A' &&
  104.             input[e] != 'e' && input[e] != 'E' &&
  105.             input[e] != 'i' && input[e] != 'I' &&
  106.             input[e] != 'o' && input[e] != 'O' &&
  107.             input[e] != 'u' && input[e] != 'U' &&
  108.             input[e] != '\0') {
  109.  
  110.             ++sum;
  111.         }  
  112.     }
  113.     cout << "The number of consonants in the string is " << sum << "." << endl;
  114.     return sum;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement