Question

In: Computer Science

Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...

Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also.

Hint: You can invoke the isalnum() function on a character, and it will return True if the string is alphanumeric, False if not. ‘5’.isalnum() -> True. You can invoke the isspace() function on a character to check if the character is a space or not.

Solutions

Expert Solution

Here is the answer for your question in Python Programming Language.

Kindly upvote if you find the answer helpful.

##########################################################

CODE :

#read user input
userInput = input("Enter a string: ")
#Initialises a string with empty value
newString = ""

#Using for loop, loops through each character of string
for eachCharacter in userInput:
#if it is an alphanumeric letter or a space
if eachCharacter.isalnum() or eachCharacter.isspace():
#adds it to the newString
newString += eachCharacter
  
print("Using Method 1 : FOR LOOP, the new string is \"{0}\"".format(newString))

newString = ""
i = 0
#Using while loop to repeat length of string times
while i < len(userInput):
#if it is an alphanumeric letter or a space
if userInput[i].isalnum() or userInput[i].isspace():
#adds it to the newString
newString += userInput[i]
i = i+1

print("Using Method 1 : WHILE LOOP, the new string is \"{0}\"".format(newString))

##################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

####################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

JAVA Write code which takes three decimal inputs from the user, creates a circle with a...
JAVA Write code which takes three decimal inputs from the user, creates a circle with a radius equal to the first input and a rectangle with length and width equal to the second and third input respectively, then prints both of these shapes. Sample run: Type a radius: 3.7 Type a length: 4.9 Type a width: 8.6 circle with radius 3.7 rectangle with length 4.9, width 8.6
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value. Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer....
Question 1: Write a program to receive a string from the user and compares the first...
Question 1: Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter. For example: Run 1: Enter a word: abcdabc First and Second Half Same Run 2: Enter a word: abcde First and Second Half Different Hint: use the Math.floor() and length() to get the...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT