Frequency of a Character: flowchart, algorithm and program
This program reads a string and a single character, then prints how many times that character appears in the string. It walks through the string one position at a time and adds 1 to a counter whenever the current letter matches the character.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Set count = 0.
- Step 3: Read str.
- Step 4: Read ch.
- Step 5: Set i = 0.
- Step 6: If str[i] != '\0', go to step 7; otherwise go to step 10.
- Step 7: If str[i] == ch, go to step 8; otherwise go to step 9.
- Step 8: Increase count by 1.
- Step 9: Increase i by 1. Go to step 6.
- Step 10: Print ch and count.
- Step 11: Stop.
Pseudocode
BEGIN
count ← 0
READ str
READ ch
i ← 0
WHILE str[i] != '\0' DO
IF str[i] == ch THEN
count++
END IF
i++
END WHILE
PRINT ch and count
END
Program in C, Python and Java
Frequency of a Character program in C
#include <stdio.h>
int main() {
char str[100];
char ch;
int i, count = 0;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter a character: ");
scanf(" %c", &ch);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
count++;
}
}
printf("Frequency of '%c' = %d\n", ch, count);
return 0;
}Frequency of a Character program in Python
str1 = input("Enter a string: ")
ch = input("Enter a character: ")
count = 0
for i in range(len(str1)):
if str1[i] == ch:
count += 1
print("Frequency of '" + ch + "' = " + str(count))Frequency of a Character program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.next();
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ch) {
count++;
}
}
System.out.println("Frequency of '" + ch + "' = " + count);
}
}
Sample input: str = programming, ch = g
Output: Frequency of 'g' = 2
How it works
- Start with count = 0, so nothing has been found yet.
- The loop runs with i from 0 until str[i] becomes '\0', the end-of-string marker, so every character is visited exactly once.
- Inside the loop, if (str[i] == ch) compares the current character with the one the user typed. On a match, count++ adds 1.
- After the loop ends, count holds the total number of matches, and it is printed along with ch.
- For "programming" and 'g', the letter g sits at index 3 and index 10, so count ends at 2.
Dry run (trace table)
| Step | Line | Box | count | str | ch | i | Output |
|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | |||||
| 2 | 6 | count = 0 | 0 | ||||
| 3 | 9 | Read str | 0 | programming | |||
| 4 | 11 | Read ch | 0 | programming | g | ||
| 5 | 13 | i = 0 | 0 | programming | g | 0 | |
| 6 | 13 | str[i] != '\0'? | 0 | g | 0 | ||
| 7 | 14 | str[i] == ch? | 0 | g | 0 | ||
| 8 | 13 | i++ | 0 | g | 1 | ||
| 9 | 13 | str[i] != '\0'? | 0 | g | 1 | ||
| 10 | 14 | str[i] == ch? | 0 | g | 1 | ||
| 11 | 13 | i++ | 0 | g | 2 | ||
| 12 | 13 | str[i] != '\0'? | 0 | g | 2 | ||
| 13 | 14 | str[i] == ch? | 0 | g | 2 | ||
| 14 | 13 | i++ | 0 | g | 3 | ||
| 15 | 13 | str[i] != '\0'? | 0 | g | 3 | ||
| 16 | 14 | str[i] == ch? | 0 | g | 3 | ||
| 17 | 15 | count++ | 1 | g | 3 | ||
| 18 | 13 | i++ | 1 | g | 4 | ||
| 19 | 13 | str[i] != '\0'? | 1 | g | 4 | ||
| 20 | 14 | str[i] == ch? | 1 | g | 4 |
Time and space complexity
Time: O(n) · Space: O(1). The loop visits each of the n characters once with constant work, using only a few extra variables.
Common mistakes
- Writing scanf("%c", &ch) without the space before %c. The leftover newline from the previous input gets read as the character, so the count is wrong (usually 0).
- Using "g" (double quotes) in the comparison instead of 'g'. Double quotes make a string, not a single character.
- Forgetting to set count = 0 before the loop, which gives a random garbage value in C.
- Expecting scanf("%s") to read a sentence with spaces. It stops at the first space, so only the first word is counted.
Viva questions
Q1. Why is count initialised to 0 before the loop?
Because we have not found any occurrence yet. count++ adds to its current value, so starting with garbage or a non-zero value would give a wrong total.
Q2. What does the loop condition str[i] != '\0' check, and why do we use it instead of a fixed length?
It checks that we have not reached the null character that marks the end of a C string. It lets the loop run exactly as many times as the string has characters, whatever its length.
Q3. Why is there a space before %c in scanf(" %c", &ch)?
The space tells scanf to skip whitespace, including the newline left in the buffer after the previous input. Without it, %c could read that newline instead of the character the user types.
Q4. What happens if the user enters the string "hello world" with a space?
scanf("%s") stops reading at the first whitespace, so only "hello" is stored in str. The characters after the space are never counted. To read a full line you would use fgets or scanf("%[^\n]").
Q5. What is the output if the character does not appear in the string, or if the string is empty?
The loop never increments count, so it prints 0. For an empty string, str[0] is '\0', so the loop body does not run at all and the output is also 0.
Q6. Is the comparison case-sensitive? What would you change to count 'a' and 'A' together?
Yes, 'a' and 'A' have different ASCII values, so they are counted separately. To ignore case, convert both str[i] and ch with tolower() (from ctype.h) before comparing them.
Q7. What is the time complexity of this program, and what is the space complexity?
Time is O(n), where n is the length of the string, because each character is visited exactly once. Space is O(1) extra, since only a few variables are used besides the input array.
Q8. What is the risk with char str[100] and scanf("%s", str), and how would you fix it?
If the user types 100 or more characters, scanf writes past the end of the array (buffer overflow). Use a field width such as scanf("%99s", str), or use fgets(str, sizeof str, stdin).
Frequently asked questions
Is the character count case-sensitive?
Yes. 'G' and 'g' are different characters, so searching for 'g' will not count 'G'. To ignore case, convert both the string character and ch to lower case (for example with tolower) before comparing.
What is the time complexity of this program?
O(n), where n is the length of the string, because each character is checked once. It uses O(1) extra space, just the count variable.
How can I count characters in a string that contains spaces?
Read the line with fgets(str, sizeof(str), stdin) instead of scanf("%s"), since %s stops at the first space. The counting loop stays the same, though you may want to ignore the trailing newline fgets keeps.