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
- The program starts by including the necessary header files:
stdio.hfor input/output operations andstring.hfor string manipulation functions. - The program defines a function named
findFirstAndLastOccurrencethat takes two parameters: the input stringstrand the character to findch. - Inside the
findFirstAndLastOccurrencefunction, the length of the stringstris calculated using thestrlenfunction from thestring.hlibrary. This length is stored in the variablelen. - Two variables,
firstandlast, are initialized to -1. These variables will keep track of the indices of the first and last occurrences of the characterchin the string. - 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.
- Inside the loop, each character of the string is checked against the target character
ch. If a match is found, the program updates thefirstvariable if it hasn’t been set yet and updates thelastvariable regardless. - After the loop, the program checks whether both
firstandlastvariables have been updated. If they have been updated, it means that the characterchwas found in the string. In this case, the program prints the indices of the first and last occurrences using theprintffunction. - If the
firstorlastvariables haven’t been updated, it means that the characterchwas not found in the string. In this case, the program prints a message indicating that the character was not found using theprintffunction. - In the
mainfunction, the program starts by declaring the necessary variables:strto hold the input string andchto store the target character. - The user is prompted to enter a string using the
printffunction. Thefgetsfunction is then used to read the input string from the user. - Next, the user is prompted to enter the character to find using another
printffunction. Thescanffunction is used to read the character. - Finally, the
findFirstAndLastOccurrencefunction is called with the provided string and character as arguments. - The program ends by returning 0 from the
mainfunction.
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
