pTutorial

New way to learn C programming

C program to count Frequency of Characters

This is a user defined c program to count the frequency of a particular character in an entered string. For example "php" is a string and frequency of p in this string is 2.

Frequency of character



#include <stdio.h>
#include <conio.h>
void main()
{
   char z[25],x;
   int i,k=0;
   
   printf(" please enter string :");
   gets(z);
   
   printf("a character: ");
   scanf("%c",&x);
   
   for(i=0;z[i]!='\0';++i)
   {
       if(x==z[i])
        k++;
   }
   
   printf("Total number of character is:%d\n",i);
   printf("Frequency of %c = %d", x, k);
   
}

Download C program to count characters frequency

Output of program

string concatenation | ptutorial

In this example first ask user for a string and then a character, simply check whether entered character is presented in the or not, if presented then increase one in the "k" variable and at last print the total number of sting and frequency of the entered character.

See Also

Php data type   |   Java array example   |   Ajax get all response header

QUOTES OF THE DAY

To make your children capable of honesty is beginning of education.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER