Count Occurrences of a Character in a String in C
In C, we can count the occurrences of a character in a string using loops, standard library functions, or pointer-based approaches. This is done by iterating through the string and keeping track of how many times the target character appears.
In this tutorial, we will explore different methods to achieve this with clear explanations and example programs.
Examples to Count Character Occurrences
1. Counting Occurrences Using a for Loop
In this example, we will traverse the string using a for loop and count the number of times a specified character appears.
main.c
#include <stdio.h>
int main() {
char str[] = "hello world";
char target = 'o';
int count = 0;
// Loop through the string to count occurrences of target character
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == target) {
count++;
}
}
printf("Character '%c' appears %d times in the string.\n", target, count);
return 0;
}
Explanation:
- We declare a string
str[] = "hello world"and a target character'o'to be counted. - We initialize a counter variable
count = 0to store the number of occurrences. - The
forloop iterates through the string until it reaches the null terminator ('\0'). - Inside the loop, we check if
str[i]is equal totarget. - If a match is found, we increment the
countvariable. - Finally, we print the total count of occurrences using
printf().
Output:
Character 'o' appears 2 times in the string.
2. Counting Occurrences Using while Loop
In this example, we will use a while loop to iterate through the string and count occurrences of a specific character.
main.c
#include <stdio.h>
int main() {
char str[] = "programming in C";
char target = 'g';
int count = 0, i = 0;
// Using while loop to count occurrences of target character
while (str[i] != '\0') {
if (str[i] == target) {
count++;
}
i++;
}
printf("Character '%c' appears %d times in the string.\n", target, count);
return 0;
}
Explanation:
- We declare a string
str[] = "programming in C"and a target character'g'. - We initialize the counter
count = 0and indexi = 0. - The
whileloop runs untilstr[i]reaches the null terminator ('\0'). - If
str[i]matchestarget, we incrementcount. - We increment
iin each iteration to move to the next character. - Finally, the count is printed using
printf().
Output:
Character 'g' appears 2 times in the string.
3. Counting Occurrences Using strchr()
In this example, we will use the strchr() function from string.h to locate occurrences of a character in a string.
main.c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "C programming is fun";
char target = 'm';
int count = 0;
char *ptr = str;
// Using strchr() to find occurrences
while ((ptr = strchr(ptr, target)) != NULL) {
count++;
ptr++; // Move to next character
}
printf("Character '%c' appears %d times in the string.\n", target, count);
return 0;
}
Explanation:
- We declare a string
str[] = "C programming is fun"and a target character'm'. - We initialize a pointer
ptrto point to the start ofstr. - The
strchr()function searches for the first occurrence oftargetand returns a pointer to it. - If a match is found, we increment
countand moveptrforward to continue searching. - The loop repeats until
strchr()returnsNULL, indicating no more occurrences. - Finally, we print the total count.
Output:
Character 'm' appears 2 times in the string.
Conclusion
We explored multiple ways to count character occurrences in a string in C:
- Using a
forloop to iterate through the string. - Using a
whileloop for iteration. - Using the
strchr()function to find occurrences efficiently.
