SHARE
TWEET

Untitled

a guest Sep 21st, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Ch10 Strings.cpp:: Created by Tae Soo Kim
  2.  
  3. #include <iostream>
  4.  
  5. int receiveString(char*& cString);
  6. int countVowels(char* cString, int size);
  7. int countConsonants(char* cString, int size);
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     char* cString = nullptr;
  14.     //the function returns size in order to dynamically allocate the size of the char array off the alphabet.
  15.     //can't use strlen on a pointer to a c-string therefore we need the size.
  16.     // for the pointer, we can just pass it by reference to the function in order to pass it by reference onto the program.
  17.     int size = receiveString(cString);
  18.     //C-String is created
  19.     //Start the menu
  20.     //Initialize input as 'F'
  21.     char input = 'F';
  22.     while (input != 'e' && input != 'E') {
  23.         cout << "A) Count the vowels in the string" << endl;
  24.         cout << "B) Count the consonants in the string" << endl;
  25.         cout << "C) Count both vowels and consonants in the string" << endl;
  26.         cout << "D) Enter another string" << endl;
  27.         cout << "E) Exit this program" << endl;
  28.         cin >> input;
  29.         if (input == 'a' || input == 'A') {
  30.             cout << "The string has " << countVowels(cString, size) << " vowels." << endl << endl;
  31.         }
  32.         else if (input == 'b' || input == 'B') {
  33.             cout << "The string has " << countConsonants(cString, size) << " consonants." << endl << endl;
  34.         }
  35.         else if (input == 'c' || input == 'C') {
  36.             cout << "The string has " << countVowels(cString, size) << " vowels and " << countConsonants(cString, size) << " consonants." << endl << endl;
  37.         }
  38.         else if (input == 'd' || input == 'D') {
  39.             // Enter another string
  40.             cin.ignore();
  41.             size = receiveString(cString);
  42.         }
  43.  
  44.     }
  45.  
  46.  
  47.    
  48. }
  49.  
  50. int receiveString(char*& cString) {
  51.     // Create an alphabet array, initialize to 0
  52.     int alphabet[256] = { 0 };
  53.     cout << "Enter a string: ";
  54.     int size = 0;
  55.     /*cin.ignore();*/
  56.     char inputChar = cin.get();
  57.     // While not null (end of str)
  58.     while (inputChar != 10) {
  59.         size++;
  60.         int aChar = inputChar;
  61.         alphabet[aChar]++;
  62.         inputChar = cin.get();
  63.     }
  64.     //Received input
  65.     //Now create the C-String
  66.     cString = new char[size];
  67.     int cStrInputIdx = 0;
  68.     for (int i = 0; i < 256; i++) {
  69.         // if we have letter
  70.         while (alphabet[i] != 0) {
  71.             char alphaChar = (char)i;
  72.             cString[cStrInputIdx] = alphaChar;
  73.             alphabet[i]--;
  74.             cStrInputIdx++;
  75.         }
  76.     }
  77.     return size;
  78. }
  79.  
  80. int countVowels(char* cString, int size) {
  81.     int vowels = 0;
  82.     for (int i = 0; i < size; i++) {
  83.         if (cString[i] == 'a' ||
  84.         cString[i] == 'A' ||
  85.         cString[i] == 'e' ||
  86.         cString[i] == 'E' ||
  87.         cString[i] == 'i' ||
  88.         cString[i] == 'I' ||
  89.         cString[i] == 'o' ||
  90.         cString[i] == 'O' ||
  91.         cString[i] == 'u' ||
  92.         cString[i] == 'U') {
  93.             vowels++;
  94.         }
  95.     }
  96.     return vowels;
  97. }
  98.  
  99. int countConsonants(char* cString, int size) {
  100.     //subtract size from vowels
  101.     int vowels = countVowels(cString, size);
  102.     return size - vowels;
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top