Here C program to count the number of occurrences of a characters/letters sequence in a string.This c program use ASCII value of small letter for looping and checking .
Example :
- String : hello
- "h" found 1 time
- "e" found 1 time
- " l" found 2 time
- "o" found 1 time
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char ch; do{ char a[20],f=0; int i,n,ascii; clrscr(); printf("enter a string:"); gets(a); strlwr(a); // Sting convert in small letter for(ascii=97;ascii<=122;ascii++)// a=97 and z=122 ascii value for looping { n=0; f=0; for(i=0;a[i]!=NULL;i++) { if(ascii==a[i])// Ascii value for checking { n++; // If Checking sucessfull ..increment +1 f=1; } } if(f==1) // Checking f value is similar or not printf("\t\t%c value found %d times ",ascii,n); } printf("\n\t continue (y/n):"); ch=getch(); }while(ch=='y'||ch=='Y'); }
Output :
If you have any Further Information and Quires about this Article don't hesitate to comment in comment box below.
AND
If you like this article , like our Facebook like page and click +1 to support 123techguide
|