62.4k views
4 votes
6.20 Program: 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:

Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

(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:

MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit

Choose an option:

(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. Call GetNumOfNonWSCharacters() in the PrintMenu() function. (4 pts)

Ex:

Number of non-whitespace characters: 181

(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:

Number of words: 35

(5) Implement the FindText() function, which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In the PrintMenu() function, prompt the user for a word or phrase to be found and then call FindText() in the PrintMenu() function. Before the prompt, callcin.ignore() to allow the user to input a new string. (3 pts)

Ex:

Enter a word or phrase to be found: more
"more" instances: 5

(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.

Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue.

(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:

Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!

User Evgenyl
by
4.3k points

1 Answer

7 votes

Answer:

See Explaination

Step-by-step explanation:

def print_menu(usr_str):

menu_op = ' '

print('MENU')

print('c - Number of non-whitespace characters')

print('w - Number of words')

print('f - Fix capitalization')

print('r - Replace punctuation')

print('s - Shorten spaces')

print('q - Quit')

print()

print('Choose an option:')

menu_op = input()

return menu_op, usr_str

def get_num_of_non_WS_characters(user_input):

count=0

for i in user_input:

if i!= ' ':

count=count+1

return count

def get_num_of_words(user_input):

word_count = len(user_input.split())

return word_count

def fix_capitalization(user_input):

char_count=0

output_string = ''

line_terminator = ['.', '!']

end_of_line = False

for i in user_input:

if output_string == '':

char_count += 1

output_string += i.upper()

elif end_of_line == False and i not in line_terminator:

output_string = output_string + i

elif i in line_terminator:

end_of_line = True

output_string = output_string + i

elif end_of_line == True and i == ' ':

output_string = output_string + i

elif end_of_line == True and i != ' ':

output_string = output_string + i.upper()

char_count += 1

end_of_line = False

return output_string, char_count

def replace_punctuation(user_input, exclamation_count, semicolon_count):

list = ['!', ';']

exclamation_count = 0

semicolon_count = 0

output = ''

for i in user_input:

if i in list:

if i == list[0]:

output += '.'

exclamation_count += 1

elif i == list[1]:

output += ','

semicolon_count += 1

elif i not in list:

output += i

return output, exclamation_count, semicolon_count

def shorten_space(user_input):

import re

output = re.sub(' +', ' ', user_input).strip()

return output

if __name__ == '__main__':

import re

valid_choices = ['c', 'w', 'f', 'r', 's', 'q']

input_str = input('Enter a sample text:\\')

print()

print('You entered:', input_str)

print()

choice, user_input = print_menu(input_str)

while choice not in valid_choices:

choice, user_input = print_menu(input_str)

if choice == 'c':

print('Number of non-whitespace characters:', get_num_of_non_WS_characters(user_input))

print()

choice, user_input = print_menu(input_str)

elif choice == 'w':

print('Number of words:', get_num_of_words(user_input))

print()

choice, user_input = print_menu(input_str)

elif choice == 'f':

edit_txt, num_letters = fix_capitalization(user_input)

print('Number of letters capitalized:', num_letters)

print('Edited text:', edit_txt)

print()

choice, user_input = print_menu(input_str)

elif choice == 'r':

output, exc_count, semi_count = replace_punctuation(user_input, exclamation_count = 0, semicolon_count = 0)

print('Punctuation replaced')

print('exclamation_count:', exc_count)

print('semicolon_count:', semi_count)

print('Edited text:', output)

print()

choice, user_input = print_menu(input_str)

elif choice == 's':

print('Edited text:', shorten_space(user_input))

print()

choice, user_input = print_menu(input_str)

elif choice == 'q':

pass

User Piyush Zalani
by
5.0k points