Algorithm


1. Start
2. Initialize a variable count to 0
3. Accept a string input from the user
4. Accept a character input from the user
5. Iterate through each character in the string:
   a. If the current character is equal to the input character:
      i. Increment the count by 1
6. Print the count
7. End

Code Examples

#1 Code Example- Phyton Program Using a for loop

Code - Python Programming

count = 0

my_string = "Programiz"
my_char = "r"

for i in my_string:
    if i == my_char:
        count += 1

print(count)
Copy The Code & Try With Live Editor

Output

x
+
cmd
2

#2 Code Example- Python Program Using method count()

Code - Python Programming

my_string = "Programiz"
my_char = "r"

print(my_string.count(my_char))
Copy The Code & Try With Live Editor

Output

x
+
cmd
2
Advertisements

Demonstration


Python Programing Example to Count the Number of Occurrence of a Character in String-DevsEnv