Last Updated on
In this article, we will see how to find the frequency of characters in a given string. This program to find character occurrence in a string is a popular question asked in technical job interview.
Given a string or list of words, you are required to find occurrence of each characters frequency.
Python code to find character occurrence in string
def findCharOccurence(str): if(len(str)==0): return "Invalid String" str_List= list(str.lower()) temp =  count=0 w, h = 2, len(str); data = [[0 for x in range(w)] for y in range(h)] j=0 while(j<len(str)): if(str_List[j] not in temp): countS= str_List.count(str_List[j]) data[j]=str_List[j] data[j]=countS temp.append(str_List[j]) count+=1 j+=1 w, h = 2, count; data2 = [[0 for x in range(w)] for y in range(h)] k=0 m=0 while(k<len(str_List)): if(data[k]!=0): data2[m]=data[k] data2[m]=data[k] m+=1 k+=1 return data2 print(findCharOccurence("tajmahal"))
In the article, you have seen how to find character occurrence in string in Python.
If in case you have doubt regarding implementation, please feel free to share us your concerns in discussion thread below, or mail us to firstname.lastname@example.org
This article is for technical interview preparation. Find more useful programming exercises in link below.
- How to check whether string is palindrome or not
- How to find prime numbers between a given range
- How to check whether number is Armstrong number
Xamnation is helping students in interview preparation and learning programming language. We are offering live classes, study material and interview preparation course. Please find below useful course.
- Aptitude preparation course for job interviews
- Puzzles course for job interviews
- Python course for job interviews
- Amazon job preparation course
- TCS job preparation course