c count number of char in string code example

Example 1: find character from string c count

#include <stdio.h>
#include <string.h>
 
int main()
{
    char s[1000],c;  
    int i,count=0;
 
    printf("Enter  the string : ");
    gets(s);
    printf("Enter character to be searched: ");
    c=getchar();
    
    for(i=0;s[i];i++)  
    {
    	if(s[i]==c)
    	{
          count++;
		}
 	}
     
	printf("character '%c' occurs %d times \n ",c,count);
 
 	 
     
    return 0;
}

Example 2: Write a c program to count the different types of characters in given string.

#include<stdio.h>
   #include<string.h> 
	void main()    {
   		char arr[]="askjdfbajksdfkasdfhjkasdfh";
   		char x;
   		int l=strlen(arr);
   		int i=0,j;
   		int c=0,var=0;
   		for(j=97;j<=122;j++) {     //a=97,z=122
   		//c=j;           
   			for(i=0;i<l-1;i++) {
   	       		if(arr[i]==j) {
   	         		c++; 
   	       		} 
   	     	} 
   	  		if(c>0) {
   	    		var++;
   	    		printf("No. of Occurence of %c ::%d\n",j,c); 
   	  		}
   	  		c=0; 
   		} 
   	  	printf("No. of type of characters:%d\n",var);   
   	}

Tags:

C Example