C program to find the first occurrence of a character in a string using while loop. | लूप का उपयोग करते हुए एक स्ट्रिंग में एक चरित्र की पहली घटना को खोजने के लिए सी कार्यक्रम।












C program to find the first occurrence of a character in a string using while loop.




C program to find the first occurrence of a character in a string using while loop.



#include <stdio.h> #include <string.h> #define MAX 100 int ind(const char * text, const char find); int main() { char text[MAX]; char toFind,find; int index; printf("\n Enter any string: "); gets(text); printf("\n Enter character to be searched in the given string: "); find = getchar(); index = ind(text, find); if(index == -1) printf("\n '%c' not found.", find); else printf("\n '%c' is found at index in string %d.", find, index); return 0; } int ind(const char * text, const char find) { int i = 0; while(text[i] != '\0') { if(text[i] == find) return i; i++; } return -1; }


Output:
Enter any string: computer Enter character to be searched in the given string: p 'p' is found at index in string 3. Enter any string: computer Enter character to be searched in the given string: q 'q' not found.



Exercise: 1
C program to find the first occurrence of a character in a string.