C Program to find a character in string


In this program, You will learn how to find a character in string in c.


String is: Xiith

character: t

Character found: t


Example: How to find a character in string in c

Run it Now
#include<stdio.h>
#include<string.h>

int main() {

   char a[100], b;
   int i = 0, len = 0, f = 0;

   printf("Enter a String :");
   fgets(a, 100, stdin);

   printf("\nEnter a Character :");
   scanf("%c", &b);

   len = strlen(a);

   for (int j = 0; j < len; j++) {
       if (a[j] == b)
           f++;
   }

   if (f == 0) {
       printf("\nCharacter Not Found :%c", b);
   } else {
       printf("\nCharacter found :%c", b);
   }

   return 0;
}

Output:

Enter a String :Xiith
Enter a Character :t
Character found :t