C Program to Find Frequency of Character in String
if (str[i] == ch) count++;.Examples
How to Think About It
Algorithm
Code
#include <stdio.h> #include <string.h> int main() { char str[100], ch; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = '\0'; printf("Enter a character to find frequency: "); scanf(" %c", &ch); for (int i = 0; str[i] != '\0'; i++) { if (str[i] == ch) { count++; } } printf("Frequency of '%c' = %d\n", ch, count); return 0; }
Dry Run
Let's trace the input string "hello" and character 'l' through the code.
Input
string = "hello", character = 'l', count = 0
Check each character
i=0: 'h' != 'l', count=0 i=1: 'e' != 'l', count=0 i=2: 'l' == 'l', count=1 i=3: 'l' == 'l', count=2 i=4: 'o' != 'l', count=2
End of string
Loop ends at i=5 (null character), final count=2
| Index | Character | Match with 'l'? | Count |
|---|---|---|---|
| 0 | h | No | 0 |
| 1 | e | No | 0 |
| 2 | l | Yes | 1 |
| 3 | l | Yes | 2 |
| 4 | o | No | 2 |
Why This Works
Step 1: Reading input
The program reads the string and the character to find from the user using fgets and scanf. It removes the trailing newline character from the input string.
Step 2: Looping through string
It uses a for loop to check each character until it finds the end marked by \0.
Step 3: Counting matches
Inside the loop, it compares each character with the target using if (str[i] == ch) and increases the count if they match.
Step 4: Displaying result
After the loop, it prints the total count of how many times the character appeared.
Alternative Approaches
#include <stdio.h> #include <string.h> int main() { char str[100], ch; int count = 0, i = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = '\0'; printf("Enter a character to find frequency: "); scanf(" %c", &ch); while (str[i] != '\0') { if (str[i] == ch) { count++; } i++; } printf("Frequency of '%c' = %d\n", ch, count); return 0; }
#include <stdio.h> int main() { char str[100]; int freq[256] = {0}; printf("Enter a string: "); fgets(str, sizeof(str), stdin); for (int i = 0; str[i] != '\0'; i++) { freq[(unsigned char)str[i]]++; } printf("Character frequencies:\n"); for (int i = 0; i < 256; i++) { if (freq[i] > 0 && i != '\n') { printf("'%c' = %d\n", i, freq[i]); } } return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program checks each character once, so time grows linearly with string length.
Space Complexity
Only a few variables are used, so space is constant regardless of input size.
Which Approach is Fastest?
Counting frequency of a single character with a loop is fastest and simplest; using an array for all characters uses more space but can be faster if many queries are needed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Single character loop | O(n) | O(1) | Counting one character frequency |
| While loop | O(n) | O(1) | Same as for loop, different style |
| Array frequency count | O(n) | O(256) | Counting all characters at once |