String

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 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

  1. Declare a character array char str[100] and character variable char ch;
  2. Declare and initialize integer variables as frequency=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. The user asked to enter a character
  6. The given caracter is stored in the variable ch;
  7. A loop(for, while and do-while) is used to count frequency of the character in the given string.
  8. It is initialized as i=0, checks the condition whether str[i]!=’\0′); and executes the loop until the given condition becomes true
  9. Use an if condition to test if(ch==str[i]), when the if statements is true,  The frequency becomes frequency + 1( frequency = frequency +1);
  10. When it is false, terminates the loop
  11. Finally, the program displays the frequency of the given character of the given string

 

 

Suggested for you

for loop in C language

while loop in C language

do-while loop in C language

 

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

 

 

 

Java program: find the frequency of the given character in a string
C++ program: find frequency of a character in the string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago