C program to find first occurrence of a character in a string

Category: C Program

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:

  1. Traverse the String: Iterate through each character of the string.
  2. Compare Characters: Check if the current character matches the target character.
  3. Return Position: If a match is found, return the position of the character.
  4. 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

  1. Include Headers:
    • stdio.h: For input/output functions.
  2. Main Function:
    • Declare Variables:
      • str[100] to store the input string.
      • ch to store the target character.
      • position initialized to 1 to indicate the character has not been found initially.
    • Input String:
      • Takes input string from the user using fgets.
    • Input Character:
      • Takes the target character from the user using scanf.
    • 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 index i and breaks out of the loop.
    • Output Result:
      • Checks the value of position. If it's not 1, prints the index of the first occurrence.
      • If position remains 1, indicates that the character is not found.

Example Walk-through

Let's illustrate this with an example:

  1. Input:
    • String: Hello, world!
    • Character: o
  2. Execution:
    • The program initializes position to 1.
    • The user inputs the string Hello, world! and the character o.
    • 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)
    • The program updates position to 4 and exits the loop.
  3. Output:
    • The program prints: The first occurrence of 'o' is at index 4.

Recommended Posts