Slip 6 - B) Write a program to accept a string and then count the occurrences of a specific character of a string.

Solution:


#include <stdio.h>
#include <string.h>
  // Driver code
int main()
{     char str[50]= "CodingActivity";
    char c ;
    int res = 0;
   printf("\n enter character ");
   scanf("%c",&c);
    for (int i=0;i<strlen(str);i++)
          // checking character in string
        if (str[i] == c)
            res++;
            printf("occurence of %c=%d",c,res++);
    return 0; 
}

Post a Comment

0 Comments