Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF

Saturday, May 3, 2014

C program to find frequency of characters in a string

This program computes frequency of characters in a string i.e. which character is present how many times in a string. For example in the string "code" each of the character 'c', 'o', 'd', and 'e' has occurred one time. Only lower case alphabets are considered, other characters (uppercase and special characters) are ignored. You can easily modify this program to handle uppercase and special symbols.

C programming code

#include <stdio.h>
#include <string.h>
void main()
   char string[100];
   int c = 0, count[26] = {0};
   printf("Enter a string\n");
   while ( string[c] != '\0' )
      /* Considering characters from 'a' to 'z' only */
    if ( string[c] >= 'a' && string[c] <= 'z' ) 
    for ( c = 0 ; c < 26 ; c++ )
     if( count[c] != 0 )
     printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);

Explanation of "count[string[c]-'a']++", suppose input string begins with 'a' so c is 0 initially and string[0] = 'a' and string[0]-'a' = 0 and we increment count[0] i.e. a has occurred one time and repeat this till complete string is scanned.

Output of program:

No comments:

Post a Comment

Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC