C program to find the first occurrence of a character in a given string – In this article, we will brief in on the multiple methods to find the first occurrence of a character in a given string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The methods used in this piece are as follows:
- Using Standard Model
- Using Function
- Using Recursion
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see with this example, you need to enter a string first up.
The string entered here is “Be yourself; everyone else is already taken”.
After that, you need to specify which character you need to search in the given string.
The character that is searched here is ‘s’.
Hence, after checking, it is confirmed that ‘s’ comes at the 7th spot in the string.
Thus, doing the same in C programming is as follows:
Using Standard Method
- Read the entered string using gets(s) and read the character to be searched using getchar() function and store the char into the variable c.
2) For loop iterates through string with the structure for(i=0;s[i];i++)
Compare the character c with s[i]
If character c matches with s[i] then initialize k=1 and come out of the loop.
3) If k=1 then print the character first occurrence at the location i.
If k!=1 then print the character is not in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <string.h> int main() { char s[1000],c; int i,n,k=0; printf("Enter the string : "); gets(s); printf("Enter character to be searched: "); c=getchar(); for(i=0;s[i];i++) { if(s[i]==c) { k=1; break; } } if(k) printf("character %c is first occurrence at location:%d ",c,i); else printf("character is not in the string "); return 0; } |
Output:
1 2 3 |
Enter the string: Be yourself; everyone else is already taken Enter character to be searched: s character s is first occurrence at location:7 |
Using Function
- The main() function calls the check() function by passing string and char as arguments to the function.
2) The function compares the char c with the elements of the string using for loop for(i=0;s[i];i++).
a) If c match with s[i] then initialize k=1 and break the loop.
b) If k=1 then the function returns i value otherwise return -1. The variable i indicates the first occurrence location of the character in the string.
3) The returned value initialized to n. The main() prints the first occurrence of the character in the given string.
If n>-1 then the main() function prints the first occurrence of the character in the given string otherwise it prints the character is not in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <stdio.h> #include <string.h> int check(char *s,char c) { int i,k=0; for(i=0;s[i];i++) { if(s[i]==c) { k=1; break; } } if(k) return i; else return -1; } int main() { char s[1000],c; int n; printf("Enter the string : "); gets(s); printf("Enter character to be searched: "); c=getchar(); n=check(s,c); if(n>-1) printf("character %c is first occurrence at location:%d ",c,n); else printf("character is not in the string "); return 0; } |
1 2 3 |
Enter the string: Be yourself; everyone else is already taken Enter character to be searched: d character d is first occurrence at location:35 |
Using Recursion
- The function check(char *s,char c)
a) Returns -1 if there is no character has existed at s[i].
b) If s[i] existed then
Compare s[i] with the character c. If both are equal then return i value.
else increase the i value and then call the function itself.
The function calls itself recursively until the character to be searched is equal to one of the characters of the string.
2) If the returned value is >-1 then print the location of the character where it occurs first in the string. Otherwise, print the character is not available in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <stdio.h> #include <string.h> int check(char *s,char c) { static int i; if(!s[i]) { return -1; } else { if(s[i]==c) return i; i++; check(s,c); } } int main() { char s[1000],c; int n; printf("Enter the string : "); gets(s); printf("Enter character to be searched: "); c=getchar(); n=check(s,c); if(n>-1) printf("character %c is first occurrence at location:%d ",c,n); else printf("character is not in the string "); return 0; } |
1 2 3 |
Enter the string: welcome to cbeginners Enter character to be searched: t character t is first occurrence at location:8 |