C Program

c program to find the first occurrence of a character in the given string

TGTeam GyaniPanditJanuary 2, 20233 min read

c program to find the first occurrence of a character in the given string

In this program, we are going to discuss how to find the first occurrence of a character in the given string.

For example-

Input-

So first step we are going to take input from the user suppose we enter a string as hello from gyani pandit

Logic-

So to check the first occurrence of a given character we are going to use for loop in it in that every character is checked and for that, we are going to use a condition statement in it.

for(i=0; i<strlen(str); i++)

if(str[i] ==ch){
flag = i;
break;

Output
according to our input string and search character, we get output as-

Enter the string: hello from gyani pandit
Enter the character that you want to search:i
The first occurrence character is i and the position is 15

// c program to find the first occurrence of a character in the given string

#include<stdio.h>
#include<string.h>
int main()
{
char str[100], ch;
int i, flag;

printf(“Enter the string :”);
scanf(“%[^n]”,str);
getchar();

printf(“Enter the character that you want to search :”);
scanf(“%c”, &ch);

for(i=0; i<strlen(str); i++)
{
if(str[i] ==ch){
flag = i;
break;
}
}
if(flag ==0){
printf(“sorry! we have not found the character %c”,ch);
}
else{
printf(“The first occurrence character is %c and position is %d”,ch,flag);
}
return 0;
}

Output

Enter the string: hello from gyani pandit
Enter the character that you want to search:i
The first occurrence character is i and the position is 15

Here are the steps to find out the last occurrence of a character from the string

Step 1: make an array of strings.

Step 2: make two integer-type variables.

Step 3: use printf() statement to send the message to the user. After that use scanf() statement to store the value of a variable.

Step 4: Use getchar() function ,

Step 5: Again use printf() function to get a character from the user, that they want to search from the given string. So to store the value of the character again use scanf() function, in that we use %c formate specifier because we get a character from the user.

Step 6: Apply the loop statement that is for loop to verify all the characters in the string.

Step 7: apply condition statement that is if statement to check whether the character entered by the user is in a string or not.

Step 8: if the character is found in the string then simply use the break statement. To print that character.

Step 9: If the character is not found in the string then we simply apply the if statement.

Step 10: If the search character is found in the string then we simply print that character and the position of that character by using printf() statement.

Step 11: Execute the program by using F9.

In this way, we simply understand how to find out the last occurrence of the string without using a function. I hope this concept is clear to you, you can also explore this concept.

Keep learning, and keep exploring!

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses