In this tutorial you will learn about the C Program to Counting All Occurrence of a Character in a String and its application with practical example.
C Program to Counting All Occurrence of a Character in a String
In this tutorial, we will learn to create a C program that will Find the First Occurrence of a Character in a String in C programming
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
What is a string?
The String is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the typeable data, it means Digits, Alphabets, Symbols, etc. A character is any single entity that is used in combination to form a word.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
1. Declaring the variables for the program. 2. Taking the input string from the user. 3. Taking the input from the user, the word needs to be searched. 4. Passing those variables to for loop. 5. Using conditional statements for the program. 6. Counting All Occurrence of a Character in a String. 7. Printing the result position. 8. End the program. |
Counting All Occurrence of a Character in a String
In this program, first, we will take the input string from the user. Then in the program, we will pass that string to a for loop and then will count all occurrences of a specific character in a String.
Let us take the example program from the below code for counting all occurrences of a Character in a String.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/** * C Program to flaging All Occurrence of a Character in a String */ #include <stdio.h> #define MAX_SIZE 100 // Maximum string size int main() { //Declaring the required variable for te program. char str[MAX_SIZE]; char toSearch; int i, flag; /*Taking the Input string from user for program */ printf("Enter any string: "); gets(str); //taking the searching character from the user printf("Enter any character to search: "); toSearch = getchar(); //Searching that character in the string flag = 0; i=0; while(str[i] != '\0') { /* * If character is found in string then * increment flag variable */ if(str[i] == toSearch) { flag++; } i++; } //printing the output numbers in the output section. printf("Total occurrence of '%c' = %d", toSearch, flag); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- str[MAX_SIZE] = it will hold the string value.
- toSearch = it will hold the character value.
- i = it will hold the integer value.
- flag = it will hold the integer value
Input string from the user for the program and take the input character from the user to search.
Counting the character in the string.
Printing output counting of the character.