14, అక్టోబర్ 2025, మంగళవారం

code to be modify ...... program 41 Here is a Python program to find the number of occurrences of each letter in a given string using a dictionary:

 def count_letter_occurrences(input_string):

    """

    Counts the occurrences of each letter in a string and stores them in a dictionary.


    Args:

        input_string: The string to analyze.


    Returns:

        A dictionary where keys are letters and values are their counts.

    """

    letter_counts = {}

    for char in input_string:

        # Consider only alphabetic characters and convert to lowercase

        if char.isalpha():

            char_lower = char.lower()

            if char_lower in letter_counts:

                letter_counts[char_lower] += 1

            else:

                letter_counts[char_lower] = 1

    return letter_counts


# Example usage:

my_string = "Hello World! This is a Test String."

occurrences = count_letter_occurrences(my_string)

print(f"The occurrences of each letter in '{my_string}' are:")

for letter, count in occurrences.items():

    print(f"'{letter}': {count}")

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి