C program to find first occurrence of a character in a string
Learn how to write a C program to find the first occurrence of a character in a given string. This step-by-step guide explains the logic and provides a detailed code implementation without using a separate function.
Finding the first occurrence of a character in a string is a common task in programming. This article will guide you through writing a C program that accomplishes this without using a separate function. We’ll discuss the logic behind the program, provide the code implementation, and explain each step in detail.
Logic Behind the Program
To find the first occurrence of a character in a string:
- Traverse the String: Iterate through each character of the string.
- Compare Characters: Check if the current character matches the target character.
- Return Position: If a match is found, return the position of the character.
- Handle No Match: If no match is found after traversing the string, indicate that the character is not present.
Write a C program to find first occurrence of a character in a string
Here's a simple C program to find the first occurrence of a character in a string without using a separate function:
#include <stdio.h>
int main() {
char str[100];
char ch;
int position = -1; // Initialize position to -1 to indicate not found
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find: ");
scanf("%c", &ch);
// Traverse the string to find the first occurrence of the character
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == ch) {
position = i; // Update position with the index of the first occurrence
break; // Exit the loop once the character is found
}
}
// Output the result
if (position != -1) {
printf("The first occurrence of '%c' is at index %d.", ch, position);
} else {
printf("The character '%c' is not found in the string.", ch);
}
return 0;
}
Output
Enter a string: learn programming at procoding
Enter a character to find: t
The first occurrence of 't' is at index 19.
Explanation of the Code
- Include Headers:
stdio.h
: For input/output functions.
- Main Function:
- Declare Variables:
str[100]
to store the input string.ch
to store the target character.position
initialized to1
to indicate the character has not been found initially.
- Input String:
- Takes input string from the user using
fgets
.
- Takes input string from the user using
- Input Character:
- Takes the target character from the user using
scanf
.
- Takes the target character from the user using
- Traverse the String:
- Uses a
for
loop to iterate through each character of the string. - Compares each character with the target character
ch
. - If a match is found, updates
position
with the indexi
and breaks out of the loop.
- Uses a
- Output Result:
- Checks the value of
position
. If it's not1
, prints the index of the first occurrence. - If
position
remains1
, indicates that the character is not found.
- Checks the value of
- Declare Variables:
Example Walk-through
Let's illustrate this with an example:
- Input:
- String:
Hello, world!
- Character:
o
- String:
- Execution:
- The program initializes
position
to1
. - The user inputs the string
Hello, world!
and the charactero
. - The program iterates through the string:
- Index 0:
H
(no match) - Index 1:
e
(no match) - Index 2:
l
(no match) - Index 3:
l
(no match) - Index 4:
o
(match found)
- Index 0:
- The program updates
position
to4
and exits the loop.
- The program initializes
- Output:
- The program prints:
The first occurrence of 'o' is at index 4.
- The program prints: