Find Last Occurrence of a Character in String in C Language

Program-Find-Last-Occurrence-of-a-Character-in-String-in-C-Language

Program Description:

Write a Program to Find the last occurrence of a character in string in c Programming language. The program should accept a string and a character from the user and find the last occurrence of the given character in the string.

This program is case-sensitive. So the uppercase characters and Lowercase characters are different.

📢 This Program is part of the C String Practice Programs series

We will look at a few methods to search for the last occurrence of a character in the string program. Each method will contain a step-by-step explanation of the program with program output. We also included the instructions to compile and run the program using your compiler.

Here are the excepted input and output.

Example Input and Output:

Input:

Enter a string : proproproprogrammer

Enter character to search in the string(Last Occurance): o

Output:

FOUND: Last Occurrence of 'o' character is found at 11 index in given string

The last occurrence of the character o is at the index 11 in the input string proproproprogrammer

Prerequisites:

It is recommended to go through the following articles to understand the program better.

We will look at the three different methods to find the last occurrences of the character in a string in C Language.

  • Iterative Method
  • User-defined Functions
  • Library Function ( strrchr library function)

Find Last Occurrence of a Character in String in C Program Explanation:

  1. Start the program by defining a Macro to hold the max size of the array – #define SIZE 100
  2. Declare the input string( inputStr) with the SIZE and the character to search (ch).
  3. Initialize a variable named lastIndex with the -1. The lastIndex variable is used to track the last occurrence of the character.
  4. Prompt the user to provide the input string( inputStr) and Read it using the gets function.
  5. Also, take the character to be searched and store the character in ch variable.
  6. To search for the Last occurrence of the character( ch) in a string( inputStr), Iterate over the string and check all characters in the string.
  7. Create a For loop by initializing i to 0. It will continue looping as long as the current character ( inputStr[i]) is not a NULL character ( '\0').
    • At each iteration, Check if the current character( inputStr[i]) is equal to ch- i.e if(inputStr[i] == ch)
    • If the above condition is true, Update the lastIndex variable with the current index( i) – i.e lastIndex = i;
    • If the condition is false, Then continue to the next iteration.
    • Increment control variable i by 1.
  8. Once the above loop is completed, Check if the lastIndex variable still has the -1 value ( lastIndex == -1), If the lastIndex value is -1, Then the character ch is NOT FOUND in the given string( inputStr).
  9. If the lastIndex value is updated, Then the character ch is FOUND in the string and the last occurrence index is stored in the lastIndex variable. Display the FOUND message on the console along with the last occurrence index.
  10. Stop the program.

Method 1: First Occurrence of a Character in String in C Program (Iterative Method):

Here is the program to search for a last occurrence character in a string in C programming language.

Program Output:

Compile the program using your compiler. We are using the GCC compiler on Ubuntu (Linux) Operating system. Here are the instructions to compile and run the programs in Linux using the GCC compiler.

gcc search-last-char.c

Run the program.

As we can see from the above output, The character l is found multiple times in the input string www.sillycodes.com. The program properly detected the last occurrence of the character l. i.e Last occurrence of the character l is found at the index 7.

Let’s check another example.

last-occurrence-of-character-in-string-program-output

In the above example, The character x is not found in the input string.

Method 2: Search for Last Occurrence of a Character in String using user-defined functions in C:

In the previous method, We have written the complete program inside the main() function. It is not a recommended method, Let’s rewrite the above program to use a function to search for the last occurrence of the character in a string.

By using the functions we can define a function once and can call it as many times as we want. Functions also increase the readability of the program and make it easier to debug issues. Here are the benefits using the functions in programming.

Here is the rewritten version of the program to search for the last occurrence of a character in a string.

We have defined a function called searchLastChar() to find the last occurrence of the character. Here is the prototype of the searchLastChar function

int searchLastChar(char *str, char ch)

This function takes two formal arguments and returns a integer value.

  • The first argument is a character pointer( char *) which pointers to the input string.
  • The second argument is character( char) to be searched.

It returns

  • -1 if the character ch is not found in the input string str
  • last index of the character ch, If it is found in the input string str.

Finally, Call the searchLastChar() function with the input string and character to be searched and display the result.

Program Output:

Let’s compile and run the program.

last-occurrence-of-character-in-string-using-user-defined-function

The character r is found in the input string and the last occurrence index is 9.

The character y is not found in the given string( programming).

Method 3: Last Occurrence of a Character in String using strrchr library function in C:

In the above method, We used a custom function to find the last occurrence of the character in a string. But the Standard C library provides a library function strrchr() to find the last occurrence of the character.

Here is the syntax of the strrchr function

[/crayon]

Here is the program to find the last occurrence of character in a string using the strrchr() function

Program Output:

Compile and Run the program.

last-occurrence-of-character-using-strrchr-function

As we can see from the above output, The program is generating the desired results.

Related String Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Find Last Occurrences of a character in a string […]

Leave a Reply