C Program to Find First and Last Occurrence of Character in a String

This C program finds the first and last occurrence of a target character in a given string and displays their indices.

Problem Statement

Given a string and a target character, we need to find and display the indices of the first and last occurrences of the target character in the string. If the target character is not found in the string, an appropriate message should be displayed.

C Program to Find First and Last Occurrence of Character in a String

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

void findFirstAndLastOccurrence(char str[], char ch)
{
    int len = strlen(str);
    int first = -1;
    int last = -1;

    for (int i = 0; i < len; i++) {
        if (str[i] == ch) {
            if (first == -1) {
                first = i;
            }
            last = i;
        }
    }

    if (first != -1 && last != -1) {
        printf("First occurrence: %d\n", first);
        printf("Last occurrence: %d\n", last);
    } else {
        printf("Character not found in the string.\n");
    }
}

int main()
{
    char str[100];
    char ch;

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

    printf("Enter a character to find: ");
    scanf("%c", &ch);

    findFirstAndLastOccurrence(str, ch);

    return 0;
}

How it Works

  1. The program starts by including the necessary header files: stdio.h for input/output operations and string.h for string manipulation functions.
  2. The program defines a function named findFirstAndLastOccurrence that takes two parameters: the input string str and the character to find ch.
  3. Inside the findFirstAndLastOccurrence function, the length of the string str is calculated using the strlen function from the string.h library. This length is stored in the variable len.
  4. Two variables, first and last, are initialized to -1. These variables will keep track of the indices of the first and last occurrences of the character ch in the string.
  5. A loop is used to iterate through each character of the string. The loop starts from index 0 and continues until it reaches the end of the string.
  6. Inside the loop, each character of the string is checked against the target character ch. If a match is found, the program updates the first variable if it hasn’t been set yet and updates the last variable regardless.
  7. After the loop, the program checks whether both first and last variables have been updated. If they have been updated, it means that the character ch was found in the string. In this case, the program prints the indices of the first and last occurrences using the printf function.
  8. If the first or last variables haven’t been updated, it means that the character ch was not found in the string. In this case, the program prints a message indicating that the character was not found using the printf function.
  9. In the main function, the program starts by declaring the necessary variables: str to hold the input string and ch to store the target character.
  10. The user is prompted to enter a string using the printf function. The fgets function is then used to read the input string from the user.
  11. Next, the user is prompted to enter the character to find using another printf function. The scanf function is used to read the character.
  12. Finally, the findFirstAndLastOccurrence function is called with the provided string and character as arguments.
  13. The program ends by returning 0 from the main function.

By following these steps, the program takes input from the user, searches for the first and last occurrences of a given character in the input string, and displays the indices of the first and last occurrences accordingly.

Input / Output

C Program to Find First and Last Occurrence of Character in a String

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...