C Program to find All Occurrence of a Character in a String

In this tutorial, i am going to show you how to find and count all occurrences of a character in a given string with the help of for loop, while loop and function.

All C Program to find All Occurrence of a Character in a String

  • C Program to find All Occurrence of a Character in a String using for loop
  • C Program to find All Occurrence of a Character in a String using while loop
  • C Program to find All Occurrence of a Character in a String using function

C Program to find All Occurrence of a Character in a String using for loop

/* C Program to find All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, count = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	for(i = 0; i <= strlen(str); i++)
  	{
  		if(str[i] == ch)  
		{
  			printf("\n '%c' is Found at Position %d ", ch, i + 1);
  			count++;
 		}
	}
	printf("\n character '%c' occurs %d times \n ",ch,count);
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String :  hello programer
Please Enter the Character that you want to Search for :  r
'r' is Found at Position 8 
 'r' is Found at Position 11 
 'r' is Found at Position 15 
 character 'r' occurs 3 times 

C Program to find All Occurrence of a Character in a String using while loop

/* C Program to find All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, count = 0;
	i = 0;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	while(str[i] != '\0')
  	{
  		if(str[i] == ch)  
		{
  			printf("\n '%c' is Found at Position %d ", ch, i + 1);
  			count++;
 		}
 		i++;
	}
	printf("\n character '%c' occurs %d times \n ",ch,count);
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String :  hello programer
Please Enter the Character that you want to Search for :  r
'r' is Found at Position 8 
 'r' is Found at Position 11 
 'r' is Found at Position 15 
 character 'r' occurs 3 times 

C Program to find All Occurrence of a Character in a String using function

/* C Program to find All Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
void Find_AllOccurrence(char str[], char ch);
int main()
{
  	char str[100], ch;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Search for :  ");
  	scanf("%c", &ch);
  	
  	Find_AllOccurrence(str, ch);
	
  	return 0;
}
void Find_AllOccurrence(char str[], char ch)
{
	int i, count = 0;
	for(i = 0; str[i] != '\0'; i++)
	{
		if(str[i] == ch)
		{
			printf("\n '%c' is Found at Position %d ", ch, i + 1);
			count++;
		}  
	}
	printf("\n character '%c' occurs %d times \n ",ch,count);
}

The result of the above c program; as follows:

Please Enter any String :  hello developer
Please Enter the Character that you want to Search for :  l
'l' is Found at Position 3 
 'l' is Found at Position 4 
 'l' is Found at Position 11 
 character 'l' occurs 3 times 

More C Programming Tutorials

Leave a Comment