C program: find the frequency of given character in a string
C program: find the frequency of given character in a string
In this article, we will discuss the concept of the C program: find the frequency of the given character in a string
In this post, we are going to learn how to find the frequency of the given character in the string in C programming language
Find the frequency of given character in a string
find the frequency of the given character in the string using for loop
The program allows the user to enter a String and then it finds the frequency of the given character in a string using for loop in C programing language
program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100],ch; int fCount=0,i; printf("Enter a string!\n"); fgets(str,sizeof(str),stdin); printf("Enter a character to find its frequency\n"); scanf("%c",&ch); for(i=0; str[i]!='\0'; i++){ if(ch==str[i]) fCount++; } printf("The frequency of %c is= %d",ch,fCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string C language Enter a character to find its frequency a The frequency of a is=2
Find the frequency of given character in a string using while loop
The program allows the user to enter a String and then it finds the frequency of the given character in a string using while loop in C programing language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char str[100],ch; int fCount=0,i; printf("Enter a string!\n"); fgets(str,sizeof(str),stdin); printf("Enter a character to find its frequency\n"); scanf("%c",&ch); i=0; while(str[i]!='\0'){ if(ch==str[i]) fCount++; i++; } printf("The frequency of %c is= %d",ch,fCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string! C programming language Enter a character to find its frequency g The frequency of g is=4
Find the frequency of given character in a string using while loop
The program allows the user to enter a String and then it finds the frequency of the given character in a string using do-while loop in C programing language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { char str[100],ch; int fCount=0,i; printf("Enter a string!\n"); fgets(str,sizeof(str),stdin); printf("Enter a character to find its frequency\n"); scanf("%c",&ch); i=0; do{ if(ch==str[i]) fCount++; i++;} while(str[i]!='\0'); printf("The frequency of %c is= %d",ch,fCount); getch(); return 0; }
When the above code is executed, it produces the following result
Enter a string! C and Java are easy languages Enter a character to find its frequency a The frequency of g is=7
Approaches
- Declare a character array char str[100] and character variable char ch;
- Declare and initialize integer variables as frequency=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- The user asked to enter a character
- The given caracter is stored in the variable ch;
- A loop(for, while and do-while) is used to count frequency of the character in the given string.
- It is initialized as i=0, checks the condition whether str[i]!=’\0′); and executes the loop until the given condition becomes true
- Use an if condition to test if(ch==str[i]), when the if statements is true, The frequency becomes frequency + 1( frequency = frequency +1);
- When it is false, terminates the loop
- Finally, the program displays the frequency of the given character of the given string
Suggested for you
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character