81,081 views
26 votes
26 votes
Write a program that removes all non-alpha characters from the given input. Ex: If the input is: -Hello, 1 world$! the output is: Helloworld

Also, this needs to be answered by 11:59 tonight.

User Jiri Kriz
by
2.8k points

2 Answers

18 votes
18 votes

Final answer:

To remove all non-alpha characters from the given input, you can use the built-in string methods in most programming languages, such as Python. The 'isalpha' method can be used to check if a character is alphabetical, and the 'join' method can be used to concatenate the valid characters into a new string.

Step-by-step explanation:

To remove all non-alpha characters from the given input, you can use the built-in string methods in most programming languages. Here's an example in Python:

def remove_non_alpha(input_string):
return ''.join(c for c in input_string if c.isalpha())

input_string = '-Hello, 1 world$!'
output = remove_non_alpha(input_string)
print(output) # Output: Helloworld

The remove_non_alpha function takes an input string and iterates over each character. It uses the isalpha method to check if a character is alphabetical. If it is, it adds it to a new string using the join method. Finally, it returns the resulting string with only alpha characters.

User Sandinmyjoints
by
2.9k points
9 votes
9 votes

Final answer:

To remove all non-alpha characters from a given input, write a program that iterates over each character and checks if it is alphabetic or not. Then, append alphabetic characters to a new string and return it.

Step-by-step explanation:

To remove all non-alpha characters from a given input, you can write a program that iterates over each character in the input and checks if it is an alphabetic character or not. If it is an alphabetic character, you append it to a new string. Here's an example program in Python:

def remove_non_alpha(input_string):
alpha_string = ''
for char in input_string:
if char.isalpha():
alpha_string += char
return alpha_string

input_string = input('Enter a string: ')print(remove_non_alpha(input_string))

This program takes user input and uses the isalpha() method to check if each character is alphabetic or not. If it is alphabetic, it appends it to the alpha_string. Finally, it returns the alpha_string without any non-alpha characters. For the given input '-Hello, 1 world$!', the output will be 'Helloworld'.

User Saeida
by
3.2k points