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

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

C program to find and count all occurrences of a character in a string; Through this tutorial, we will learn how to find and count all occurrences of a character in a given string using for loop, while loop and function.

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

  • 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 output 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] != '
/* 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;
}
') { 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 output 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] != '
/* 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);
}
'; 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 output 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 

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 *