A company has launched a new text editor that allows users to enter English letters, numbers and whitespaces only. If a user attempts to enter any other type of character, it is counted as a miss. Given a string of text, write an algorithm to help the developer detect the number of misses by a given user in the given input.
Input
The input consists of a string textinput, representing the text that is entered in the text editor by the user.
Output
Print an integer representing the number of misses by a given user in the given input.
Example
Input:
aa a234bc@ sads hsagd^
Output:
3
Explanation:
The characters that are counted as misses by the editor are l’@’s.]
Solution
def editorMisses(textinput):
count = 0
for i in textinput:
if i.isalpha() == False and i.isdigit() == False and i.isspace() == False:
count += 1
return count
public static int editorMisses(String input) {
int miss = 0;
for (int i = 0; i < input.length(); i++) {
if (!Character.isLetterOrDigit(input.charAt(i)) && !Character.isWhitespace(input.charAt(i))) {
miss++;
}
}
return miss;
}
Happy Learning – If you require any further information, feel free to contact me.