C program to search all occurrences of a character in a string

Category: C Program

Learn how to write a C program to search for all occurrences of a character in a given string. This step-by-step guide includes a detailed explanation and code implementation, perfect for beginners in C programming.

In this article, we will walk through a C program to search for all occurrences of a specified character in a given string. This task is fundamental in text processing, where identifying the position of characters can be crucial for various applications such as data parsing, string manipulation, and pattern recognition.

Objective

The goal is to create a C program that accepts a string and a character as inputs, and then searches for all occurrences of that character in the string, returning their positions.

Example
-------

Enter a string: learn programming at ProCoding
Enter the character to search for: a
Character 'a' found at positions: 2 11 18

Enter a string: learn programming at ProCoding
Enter the character to search for: z
Character 'z' not found in the string.

Approach

To solve this problem, we will:

  1. Take a string input from the user.
  2. Take a character input to search for within the string.
  3. Iterate through the string to find all occurrences of the specified character.
  4. Print the positions of each occurrence.

Detailed Steps

  1. Read the input string and character:
    • Use standard input functions to get the string and the character to be searched.
  2. Iterate through the string:
    • Loop through each character in the string.
    • Compare each character with the specified search character.
  3. Store and print the positions:
    • If a match is found, store the position (index) of the character.
    • After the loop, print all the stored positions.

Write a C program to search all occurrences of a character in a string

Below is the complete C program that implements the above steps:

#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    char search_char;
    int positions[100];
    int pos_count = 0;

    // Input the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character from string if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }

    // Input the character to search for
    printf("Enter the character to search for: ");
    scanf("%c", &search_char);

    // Search for the character in the string
    for (int i = 0; i < strlen(str); i++) {
        if (str[i] == search_char) {
            positions[pos_count] = i;
            pos_count++;
        }
    }

    // Print the positions of the character
    if (pos_count > 0) {
        printf("Character '%c' found at positions: ", search_char);
        for (int i = 0; i < pos_count; i++) {
            printf("%d ", positions[i]);
        }
        printf("\n");
    } else {
        printf("Character '%c' not found in the string.", search_char);
    }

    return 0;
}

Output

Enter a string: learn programming at ProCoding
Enter the character to search for: a
Character 'a' found at positions: 2 11 18

Explanation of the Program

  1. Input Handling:
    • The program uses fgets() to read a line of text from the user, ensuring that spaces are included.
    • It then removes any trailing newline character added by fgets().
  2. Character Search:
    • A for loop iterates over each character in the string.
    • If a character matches the search character, its position is stored in an array.
  3. Output:
    • After the loop, the program checks if any positions were recorded.
    • If matches are found, it prints the positions. Otherwise, it informs the user that the character was not found.

Recommended Posts