Question

‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

‘C’ programming language question

Write a MENU DRIVEN program to

A) Count the number of vowels in the string

B) Count the number of consonants in the string

C) Convert the string to uppercase

D) Convert the string to lowercase

E) Display the current string

X) Exit the program

The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case or capital letter both.

The string input may be a long sentence, with the newline indicating the end of the string. Define the character array as a global array that may be used amongst the different functions, so the functions would be void and no parameters passed to them

Assume that the string is max 100 characters, so declare your array accordingly.

When the A or B commands are entered (counting vowels or consonants), call the Corresponding Function, then print the result from main()

When the C or D commands are chosen, just call the appropriate function to convert the string. Ensure that output is done in main function again.

When E is chosen, print the contents of the stored string.

There are 4 functions needed to be defined. Each of these functions should have a single parameter -- accepting a c-style string as an argument. The function should only do what is specified (note that none of these functions do any output to the screen). You must define prototype for the functions. Function 1 and 2 return int and Function 3 and 4 are void functions.

Write a function that counts and returns the number of vowels in the string. (For the purposes of this exercise, we are talking about the standard 5 vowels -- A, E, I, O, U).

Write a function that counts and returns the number of consonants in the string.

Write a function that converts the string to all lowercase.

Write a function that converts the string to all uppercase.

Sample Run

Please Input text: The quick brown fox jumped. The lazy dog, he was jumped over.

A) Count the number of vowels in the string

B) Count the number of consonants in the string

C) Convert the string to uppercase

D) Convert the string to lowercase

E) Display the current string

X) Exit the program

Make a selection: a

Number of vowels: 16

Make a selection: B

Number of consonants: 31

Make a selection: c

Make a selection: e

The string:

THE QUICK BROWN FOX JUMPED. THE LAZY DOG, HE WAS JUMPED OVER.

Make a selection: D

Make a selection: E

The string:

the quick brown fox jumped. the lazy dog, he was jumped over.

Enter your menu selection: x

Goodbye

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Notes:

I took the liberty of adding a few helper functions to make the program a lot elegant. If any issue comes up or you wish to know something about this program do comment. Have a great day!

Code:

#include <stdio.h>
#include <stdlib.h>

const size_t MAX_SIZE = 100;
char *userInputText;

void PrintTotalVowels();
void PrintTotalConsonants();
void PrintUppercase();
void PrintLowercase();
int ShowMenuAndGetResponse();
void die();

int main()
{
int totalReadCharacters;
int menuSelection;

// allocate memory for character array aka string.
userInputText = (char *)malloc(MAX_SIZE*sizeof(char));

printf("Please Input text: ");
totalReadCharacters = getline(&userInputText, (size_t *)&MAX_SIZE, stdin);

menuSelection = ShowMenuAndGetResponse();

switch (menuSelection) {
case 1:
PrintTotalVowels();
break;
case 2:
PrintTotalConsonants();
break;
case 3:
PrintUppercase();
break;
case 4:
PrintLowercase();
break;
case 5:
printf("%s\n", userInputText);
break;
case 6:
printf("Buh Bye!\n");
}

return 0;
}


/**
* Print total number of vowels to screen.
*/
void PrintTotalVowels()
{
int totalVowels = 0;

for (int i = 0; i < MAX_SIZE; ++i) {
switch (userInputText[i]) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
++totalVowels;
break;
}
}

printf("\nNumber of vowels: %i\n", totalVowels);
return;
}

/**
* Print total number of consonants to screen.
*/
void PrintTotalConsonants()
{
int totalConsonants = 0;

for (int i = 0; i < MAX_SIZE; ++i) {
switch (userInputText[i]) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
break;
default:
++totalConsonants;
}
}

printf("\nNumber of consonants: %i\n", totalConsonants);
return;
}


/**
* Converts all characters to lowercase.
*/
void PrintLowercase()
{
char internalString[MAX_SIZE];

for (int i = 0; i < MAX_SIZE; ++i) {
if (userInputText[i] > 64 && userInputText[i] < 91)
internalString[i] = userInputText[i]+32;
else
internalString[i] = userInputText[i];
}

printf("\nThe string: %s\n", internalString);
return;
}


/**
* Converts all characters to uppercase.
*/
void PrintUppercase()
{
char internalString[MAX_SIZE];

for (int i = 0; i < MAX_SIZE; ++i) {
if (userInputText[i] > 96 && userInputText[i] < 123)
internalString[i] = userInputText[i]-32;
else
internalString[i] = userInputText[i];
}

printf("\nThe string: %s\n", internalString);
return;
}

/**
* Shows menu to user and gets a response.
*/
int ShowMenuAndGetResponse()
{
char res;

printf("A) Count the number of vowels in the string\nB) Count the number of consonants in the string\nC) Convert the string to uppercase\nD) Convert the string to lowercase\nE) Display the current string\nX) Exit the program\n");
printf("Make a selection: ");
scanf("%c", &res);

if ((res > 64 && res < 70) || res == 88)
return res - 64;
else if ((res > 96 && res < 102) || res == 120)
return res - 96;
else
die();
}

/**
* Kills program
*/
void die()
{
printf("Error: Improper selection!\n");
exit(1);
return;
}

Output:

Add a comment
Know the answer?
Add Answer to:
‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need this in Visual Studio C++ Write a function that count the number of vowels,...

    I need this in Visual Studio C++ Write a function that count the number of vowels, the number of consonants and the average number of letters in each word. The function accept a C-String (char array) or a string object as an argument and return the number of vowels, number of consonants and the average number of letters in each word. Problem: Requirements: . Use pointers as part of the solution Read a string from a text file. . Write...

  • Write a python program that does the following: takes as input from the user an English...

    Write a python program that does the following: takes as input from the user an English sentence calls the function vc_counter() that: takes a string argument counts the number of vowels and consonants in the string returns a dictionary of the counts, using the keys total_vowels and total_consonants Uses the return from vc_counter() to print the total vowels and consonants with appropriate descriptions. Example: Enter an English sentence: The quick brown fox's jump landed past the lazy dog! Total #...

  • Simple Python Program Working with strings Write a Python program to read in a number of...

    Simple Python Program Working with strings Write a Python program to read in a number of strings from the user. Stop when the user enters “Done”. Then check if each of the strings contains all the letters of the alphabet. Print the results in the form of a dictionary, where they keys are the strings and the values are the Truth Value for the string, which you just calculated. Sample Run: Enter the strings: taco cat The quick brown fox...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • I/O program for C Write a program in direct1.c which reads from standard input a line...

    I/O program for C Write a program in direct1.c which reads from standard input a line and then outputs that line immediately to standard output. it should read the first line only ! Then, write another program called direct2.c which reads from standard input every line until end of input and outputs them to standard output. Everything that goes in should come out exactly as it was. Compile both programs and run them on the input files given below The...

  • Java: Assume letters A E O U I as the vowels. Write a program that prompts...

    Java: Assume letters A E O U I as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string. The same vowels or consonants are counted only once. Use sets in the code.

  • 1. Create a Python script file called Assignment_Ch06-02_yourLastName.py. (Replace yourLastName with your last name.) 2. Write...

    1. Create a Python script file called Assignment_Ch06-02_yourLastName.py. (Replace yourLastName with your last name.) 2. Write a Python program that prompts the user for a sentence, then replaces all the vowels in the sentence with an asterisk: '*' Your program should use/call your isVowel function from Assignment_Ch06-01. You can put the isVowel() function in a separate .py file, without the rest of the Ch06-01 code, and then import it. 6-01 CODE: def isVowel(x):     if x in "aeiouyAEIOUY":         return True     else:...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • C programming (not c++) This programs input is a series of words. All words consist of...

    C programming (not c++) This programs input is a series of words. All words consist of only lowercase letters(a-z), no uppercase letters or digits or punctuation or other special symbols. The program reads until end-of-file, and then prints out the lowercase letters that were not seen in the input. Enter your input: the quick brown fox jumps over the lazy old dog Missing letters: enter your input: roll tide missing letters: a b c f g h j k m...

  • (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O...

    (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O and U are the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop:  Read one line from the input file and store it in a string;  Count the number of vowels and consonants (using either while-loop or for-loop) in the file string. The while-loop...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT