# Find the most frequent character in a sentence and print the letter and then the frequency., not just ['e' , 5]

Question
3 views

Find the most frequent character in a sentence and print the letter and then the frequency.

, not just ['e' , 5]

check_circle

Step 1

The python program and approach are given below:

Approach:

• Defining the value of size variable as per the ASCII table.
• Defining the function mostfrequentchar(s) which returns the most frequent string and its count.
• Declaring the variables as per the requirement (use of each variable is mentioned as comments in the program).
• First for loop will traverse the string and update the list with the ASCII values + 1 at every character.
• Second, for loop will traverse the string and if the statement will check whether the value of num[ord(x)] is greater than If max is less than the value of max will update.
• Most frequent character in the string will store in the variable “y”.
• Returns y and max.
• Displays the result by calling the function "mostfrequentchar()" inside the print() function.
Step 2

Program:

#setting the limit as per the ASCII table

size = 128

#defining the function which returns

#most frequent character and its count

def mostfrequentchar(s):

#setting the size of list

num =  * size

#to store the count of most frequent character

max = -1

#empty string

y = ''

#list is update with the ASCII values + 1 at every character

for x in s:

num[ord(x)]=num[ord(x)]+1;

#traversing the string

for x in s:

#if the value of max is greater than num[ord(x)]

#than store it t...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

### Python 