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