In C code ( UNIX C CODE), please Authoring assistant (C) (1) Prompt the user to
ID: 3857544 • Letter: I
Question
In C code ( UNIX C CODE), please
Authoring assistant (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
1
char* AcceptText() {
char* input = malloc(sizeof(char) * 1024);
printf("Enter a sample text: ");
scanf("%[^ ]",input);
printf("You entered: %s ",input);
return input;
}
2
char PrintMenu(char *s) {
char ch;
char *s = AcceptText();
printf("MENU c - Number of non-whitespace characters w - Number of words f - Fix capitalization
r - Replace all !'s s - Shorten spaces q - Quit Choose an option: ");
scanf("%c",&ch);
switch (ch) {
case 'c':
printf("Number of non-whitespace characters: %d ", GetNumOfNonWSCharacters(s);
break;
case 'w':
printf("Number of words: %d ", GetNumOfWords(s);
break;
case 'f':
FixCapitalization(s);
printf("Edited text: %s ",s);
break;
case 'r'
ReplaceExclamation(s);
printf("Edited text: %s ",s);
break;
case 's'
ShortenSpace(s);
printf("Edited text: %s ",s);
break;
}
return ch;
}
int main() {
char ch;
do {
ch = PrintMenu();
}while(ch != 'q' || ch != 'Q');
}
3
int GetNumOfNonWSCharacters(char *s) {
int i, count = 0;
for (i = 0; s[i] != ''; i++)
if (s[i] != ' ' && s[i] != ' ')
count ++;
return count;
}
4.
int GetNumOfWords(){
int i, count = 1, flag = 1;
for (i = 0; s[i] != ''; i++)
if (s[i] == ' ' || s[i] == ' ' || s[i] == '.' || s[i] == '?' || s[i] == '!' || s[i] == ',') {
if(flag) { //flag to ignore concurent occurance of symbols
count ++;
flag = 0;
}
}
else
flag = 1;
return count;
}
5.
void FixCapitalization(char *s) {
int i, flag = 1;
for (i = 0; s[i] != ''; i++)
if (s[i] == '.' || s[i] == '?' || s[i] == '!')
flag = 1
else if(flag && s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 32;
flag = 0;
}
}
6.
void ReplaceExclamation(char *s) {
int i;
for (i = 0; s[i] != ''; i++)
if (s[i] == '!')
s[i] == '.';
}
7.
void ShortenSpace(char *s) {
int i, j, flag = 1;
for (i = 0; s[i] != ''; i++)
if (s[i] == ' ')
if (flag)
for(j = i--; s[j]!= ''; j++)
s[j] = s[j+1];
else
flag = 1;
else
flag = 0;
}
You are requested not to post questions with more than 4 sub qustions. I hope you like the code!!