GithubHelp home page GithubHelp logo

analyze-a-string's Introduction

Analyze-a-String

Analyze a user entered string. #include #include using namespace std;

int countVowels(char *, int); // Function prototype int countLetters(char *, int); int countNumbers(char *, int);

int main () { const int SIZE = 51; char userString[SIZE];

cout << "\nEnter a string (up to 50 characters): "; cin.getline(userString, SIZE);

//************************** // Constants for menu choices const int COUNT_VOWELS = 1, COUNT_CONS = 2, COUNT_NUM = 3, COUNT_LETTERS = 4, ENTER_NEW_LINE = 5, QUIT_CHOICE = 6; // Variables int choice;

do { //Display the menu. cout << "\n\tSearching and Counting Characters from a String Using Pointers Menu\n\n" << "1. Count Vowels\n" << "2. Count Consonants\n" << "3. Count Numbers\n" << "4. Count Letters\n" << "5. Enter New Line\n" << "6. Quit Program\n\n" << "Enter your choice: "; cin >> choice; // Validate the menu selection. while (choice < COUNT_VOWELS || choice > QUIT_CHOICE) { cout << "Please enter a valid menu choice: "; cin >> choice; }

switch (choice) { //Menu item 1 case COUNT_VOWELS: cout << "There are " << countVowels(userString, SIZE) << " vowels.\n"; break; //Menu item 2 case COUNT_CONS: cout << "There are " << countLetters(userString, SIZE)-countVowels(userString, SIZE) << " consonants.\n"; break; //Menu item 3 case COUNT_NUM: cout << "There are " << countNumbers(userString, SIZE) << " numbers.\n"; break; //Menu item 4 case COUNT_LETTERS: cout << "There are " << countLetters(userString, SIZE) << " letters.\n"; break; //Menu item 5 case ENTER_NEW_LINE: cout << "\n*****************************************\n\nEnter a string (up to 50 characters): "; cin >> userString; break; }

cout << "End of iteration\n\n"; } while (choice != QUIT_CHOICE);

cout << "End of program"; return 0; } //End of Main Function *****************************************

//Function Count Vowels **************************************** int countVowels(char *strPtr, int SIZE) { int vowels = 0;

// Step through the string counting vowels while (*strPtr != '\0') { if (*strPtr == 'a' || *strPtr == 'e' || *strPtr =='i' || *strPtr =='o' || *strPtr =='u' || *strPtr =='A' || *strPtr =='E' || *strPtr =='I' || *strPtr =='O' || *strPtr =='U') vowels++;

strPtr++; } return vowels; }

//Function Count Consonants ************************************** int countLetters(char *strPtr, int SIZE) { int letters = 0; // Number of letters

// Step through the string counting letters while (*strPtr != '\0') { if (isalpha(*strPtr)) letters++;

strPtr++; } return letters; }

//Function Count Numbers ************************************** int countNumbers(char *strPtr, int SIZE) { int numbers = 0; // Number of letters

// Step through the string counting letters while (*strPtr != '\0') { if (isdigit(*strPtr)) numbers++;

strPtr++; } return numbers; }

analyze-a-string's People

Contributors

cgillie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.