Computer Science, asked by Tajinder4262, 10 hours ago

(str) take the str parameter being passed, which will contain only alphabetic character and spaces, and return the first non repeating character. For eg. If str is "agettkgaeee" then your program should return k. The string will always contain at least one character and there will always be at least one non repeating character.

Answers

Answered by Equestriadash
13

The following co‎des have been written using Python.

\tt d ef\ non\_rep\_char(string):\\{\ \ \ \ \ }c\ =\ 0\\{\ \ \ \ \ }for\ i\ in\ string:\\{\ \ \ \ \ }{\ \ \ \ \ }if\ i.isalpha():\\{\ \ \ \ \ }{\ \ \ \ \ }{\ \ \ \ \ }if\ string.count(i)\ ==\ 1:\\{\ \ \ \ \ }{\ \ \ \ \ }{\ \ \ \ \ }c\ =\ i\\{\ \ \ \ \ }{\ \ \ \ \ }{\ \ \ \ \ }break\\{\ \ \ \ \ }print(c)\\

We define a function using \tt d ef that has been designed for the same purpose. We assign a variable to be used as the variable that holds the non-repeating character. We then start an iteration loop to traverse through the characters of the string. As per the question, the string must contain only letters/spaces. We use \tt is.alpha() to check if the character is a letter through a conditional statement. If it is, we give another conditional statement to check the occurrences of the character in the string using \tt count(). If the count of the character in the string is 1, we assign it to the variable that is to represent the final output and then break the loop as the question asks for the first non-repeating character. The final output is then printed.

Similar questions