Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is C. Please write it C. 1) Prompt the user to enter a string of their choo

ID: 3804456 • Letter: T

Question

This is C. Please write it C.

1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt)

Ex:


(2) Implement a PrintMenu() function, which has a string as a parameter, outputs a menu of user options for analyzing/editing the string, and returns the user's entered menu option. Each option is represented by a single character.

If an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to call PrintMenu() until the user enters q to Quit. (3 pts)

Ex:


(3) Implement the GetNumOfNonWSCharacters() function. GetNumOfNonWSCharacters() has a constant string as a parameter and returns the number of characters in the string, excluding all whitespace. Hint: Using fgets() to read input will cause your string to have a newline character at the end. The newline character should not be counted by GetNumOfNonWSCharacters(). Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)

Ex:


(4) Implement the GetNumOfWords() function. GetNumOfWords() has a constant string as a parameter and returns the number of words in the string. Hint: Words end when a space is reached except for the last word in a sentence. Call GetNumOfWords() in the PrintMenu() function. (3 pts)

Ex:


(5) Implement the FixCapitalization() function. FixCapitalization() has a string parameter and updates the string by replacing lowercase letters at the beginning of sentences with uppercase letters. FixCapitalization() DOES NOT output the string. Call FixCapitalization() in the PrintMenu() function, and then output the edited string. (3 pts)

Ex:


(6) Implement the ReplaceExclamation() function. ReplaceExclamation() has a string parameter and updates the string by replacing each '!' character in the string with a '.' character. ReplaceExclamation() DOES NOT output the string. Call ReplaceExclamation() in the PrintMenu() function, and then output the edited string. (3 pts)

Ex.


(7) Implement the ShortenSpace() function. ShortenSpace() has a string parameter and updates the string by replacing all sequences of 2 or more spaces with a single space. ShortenSpace() DOES NOT output the string. Call ShortenSpace() in the PrintMenu() function, and then output the edited string. (3 pt)

Ex:

Explanation / Answer

#include <stdio.h>

#define MAX 1000

char PrintMenu(char str[MAX])
{
char c;
printf(" For your string: %s ", str);
printf("c - Number of non-whitespace characters ");
printf("w - Number of words ");
printf("f - Fix capitalization ");
printf("r - Replace all !'s ");
printf("s - Shorten spaces ");
printf("q - Quit ");
printf("Choose an option: ");
scanf(" %c", &c);
return c;
}

int GetNumOfNonWSCharacters(const char str[MAX])
{
int count = 0;
for(int i=0;str[i]!='';i++)
{
if(str[i]!=' '&&str[i]!=' ')
count++;
}
return count;
}

int GetNumOfWords(const char str[MAX])
{
int count = 1, first = 0;

for(int i=1;str[i]!='';i++)
{
if(str[i]!=' '&&str[i]!=' '&&first==1)
{
count++;
first = 0;
}else if(str[i]==' '||str[i]==' ')
first = 1;
}

return count;
}

int main()
{

//part 1
char str[MAX];
printf("Enter a sample string: ");
scanf("%[^ ]s", str);
printf("You entered: %s ", str);


//part 2
char choice;
int result;
do
{

choice = PrintMenu(str);

switch(choice)
{
case 'c': result = GetNumOfNonWSCharacters(str);
printf("Number of non white space characters are %d ", result);
break;
case 'w': result = GetNumOfWords(str);
printf("Number of words are %d ", result);
break;
case 'q': continue;
default: printf("Option invalid ");
}

}while(choice!='q');


printf(" ");

return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote