C Program to Count All Occurrences of a Character in a String

C Program to Count All Occurrences of a Character in a String

C program to count all occurrences of a character in a string; Through this tutorial, we will learn how to count number of occurrences of a character in a string using for loop, while loop, recursion and functions in c programs.

Programs to Count All Occurrences of a Character in a String in C

  • C Program to Count All Occurrences of a Character in a String using For Loop
  • C Program to Count All Occurrences of a Character in a String using While Loop
  • C Program to Count All Occurrences of a Character in a String Using Recursion
  • C Program to Count All Occurrences of a Character in a String using Function

C Program to Count All Occurrences of a Character in a String using For Loop

#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;
}

The Output of the above c program; as follows:

Enter  the string : welcome to programmer world
Enter character to be searched: o
character 'o' occurs 4 times 

C Program to Count All Occurrences of a Character in a String using While Loop

/* C Program to Count All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, Count;
  	i = Count = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("Enter character to be searched: ");
  	
  	scanf("%c", &ch);
  	
  	while(str[i] != '
/* C Program to Count All Occurrence of a Character in a String */
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, Count;
i = Count = 0;
printf("\n Please Enter any String :  ");
gets(str);
printf("Enter character to be searched: ");
scanf("%c", &ch);
while(str[i] != '\0')
{
if(str[i] == ch)  
{
Count++;
}
i++;
}
printf("\n The Total Number of times '%c' has Occured = %d ", ch, Count);
return 0;
}
') { if(str[i] == ch) { Count++; } i++; } printf("\n The Total Number of times '%c' has Occured = %d ", ch, Count); return 0; }

The Output of the above c program; as follows:

Please Enter any String :  welcome to c programming world
Enter character to be searched: c
The Total Number of times 'c' has Occured = 2 

C Program to Count All Occurrences of a Character in a String Using Recursion

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

The Output of the above c program; as follows:

Enter  the string : hello test
Enter character to be searched: t
character 't' occurs 2 times 
 

C Program to Count All Occurrences of a Character in a String using Function

/* C Program to Count All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>

int Count_AllCharcater(char str[], char ch);
 
int main()
{
  	char str[100], ch;
  	int Count;
  	Count = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
    printf("Enter character to be searched: ");
  	scanf("%c", &ch);
  	
  	Count = Count_AllCharcater(str, ch);
  	
	printf("\n The Total Number of times '%c' has Occured = %d ", ch, Count);
	
  	return 0;
}

int Count_AllCharcater(char str[], char ch)
{
	int i, Count;
	Count = 0;
	
  	for(i = 0; i <= strlen(str); i++)
  	{
  		if(str[i] == ch)  
		{
  			Count++;
 		}
	}
	return Count;
}

The Output of the above c program; as follows:

Please Enter any String :  test
Enter character to be searched: t
The Total Number of times 't' has Occured = 2 

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *