c program to find the number of occurences of a letter in a string

Explanation of the program



This program finds the number of occurrence of a letter in a string. The string is read into character array a[] and the letter to be searched is read into variable b. The while loop uses the loop variable i, which is initially set to 0. The loop continues until a[i]= '\0', (it marks the end of string). At each iteration in the while loop, the value of a[i] is compared with the value of b, if it equals the value of count is incremented. After exiting from the while loop the value in the variable count gives the total number of occurrence of the letter in the string.

Program



// c strings - number of occurrence of a letter

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i=0,count=0;

char a[10];

char b[10];

printf("Enter the string");

scanf("%s",&a);

printf("Enter the letter");

scanf("%s",&b);

while(a[i]!='\0')

{

if(a[i]==b[0])

{

count++;

}

i++;

}

printf("%d",count);

getch();

}


Output



Number of occurrence of a letter in a string
C program to find the number of occurrence of a letter in a string





No comments: