26.2k views
5 votes
1. INTRODUCTION In this project, you will gain experience with many of the Python elements you’ve learned so far, including functions, repetition structures (loops), input validation, exception handling. and working with strings 2. PROBLEM DEFINITION Present the user with the following menu within a continuous loop. The loop will exit only when the user selects option 7. 1. Enter a string 2. Display the string 3. Reverse the string 4. Append a string to the existing one 5. Slice the string 6. Display the number of occurrences of each letter 7. Quit Program operation: Option 1: Prompt the user to enter a string. Option 2: Display the string to the user. Option 3: Reverse the string and display it to the user. Option 4: Prompt the user to enter a string, then append (i.e. concatenate) it to the end of the existing string. Option 5: Prompt the user for the first and second integers of a slice operation, then replace the existing string with a sliced version of it. Option 6: Print on a separate line each alphabetic character contained in the string. Don’t worry about punctuation, special characters, or whitespace. The letters are not to be considered case-sensitive, so you are free to display them either upper or lower case. Along with each letter, display number of occurrences within the string. For example, if the string is "Hello, World", the output would be: h, 1 e, 1 l, 3 o, 2 w, 1 r, 1 d, 1

1 Answer

2 votes

Answer:

See Explaination

Step-by-step explanation:

def process_string(s):

global char#global varibles

char=[]

global count

count=[]

for i in s:

#checking whether i is alphabet or not

if(i.isalpha()):

#converting to lower

i=i.lower()

if i in char:

#if char already present increment count

count[char.index(i)]+=1

else:

#if char not present append it to list

char.append(i)

count.append(1)

#menu

print("1. Enter a string")

print("2. Display the string")

print("3. Reverse the string")

print("4. Append a string to the existing one")

print("5. Slice the string")

print("6. Display the number of occurences of each letter")

print("7. Quit")

op=input("Enter option:")

#as I don't know what you have covered

#I am trying to make it simple

#checking whether input is numeric or not

#you can use try except if you want

while(not op.isnumeric()):

op=input("Enter valid option:")

op=int(op)

global x

while(op!=7):

if(op==1):

x=input("Enter a string: ")

elif(op==2):

print("The string is:",x)

elif(op==3):

x=x[::-1]#reversing the string

print("The string is:",x)

elif(op==4):

y=input("Enter a string: ")

x=x+y #string concatnation

elif(op==5):

a=input("Enter first integer: ")

while(not a.isnumeric()):

a=input("Enter valid first integer: ")

a=int(a)

b=input("Enter second integer: ")

while(not b.isnumeric()):

b=input("Enter valid second integer: ")

b=int(b)

x=x[a:b]#string slicing

#you can also use x.slice(a,b)

elif(op==6):

process_string(x)

for i in range(len(char)):

print(char[i],",",count[i])

else:

#incase of invalid input

print("Invalid option")

print("1. Enter a string")

print("2. Display the string")

print("3. Reverse the string")

print("4. Append a string to the existing one")

print("5. Slice the string")

print("6. Display the number of occurences of each letter")

print("7. Quit")

op=input("Enter option:")

while(not op.isnumeric()):

op=input("Enter valid option:")

op=int(op)

User Suresh Sekar
by
4.0k points