Latest :

C Program To Find Last Occurrence Of A Character In A Given String

C program to find the last occurrence of a character in a given string – In this article, we will describe the numerous means to find the last occurrence of a character in a given string in C programming.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The means used in this piece are as follows:

  • Using Standard Method
  • Using Function
  • Using Recursion

A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.

In the image uploaded above, it is seen that you need to enter the string first up.

The string that has been entered here is as follows:

“Be yourself; everyone else is already taken”

After that, you need to enter the character that needs to be searched.

The character ‘e’ is assigned in the given example.

It is seen that the character ‘e’ last occurs at location 41.

Thus, doing the same in C programming is as follows:

Using Standard Method

  1. Read the entered string using gets(s).

2) Read the entered character to be searched and store that character into the variable c.

3)For loop iterate through string in reverse order  with the structure for(i=strlen(s)-1;i>=0;i–).

compare s[i] with the character c.if both are equal then initialize k=1 and break the loop.

4) If k=1 then print the location of the character where its last occurrence in the string. Otherwise, print the character is not available in the string.

Output:

Using Function

  1. The main() calls the check(char *s, char c) function to find the last occurrence of the given character in the string.

2) The for loop iterates through the string in reverse order with the structure for(i=length of the string-1; i>=0; i–).

comparing the given character with the elements of the string in reverse order.

a) If the character at s[i] matched with the given character then initialize k=1 and break the loop and return the i value.

b) Otherwise, decrease the i value.

Repeat this until finds the occurrence of the character.

3) Print the return value i which is the last occurrence of the character.

Output:

Using Recursion

  1. The main() calls the recursive function check(char *s, char c) to find the last occurrence of the character.

2) The function check(char *s,char c)

a) If the length of the string-1<0 then it returns i value.

b) Otherwise,

b.1) Compare the given character with the element at the last index “string length-1”.If matched then return ‘i’ value which is the last occurrence of the character.

b.2) If not matched then decrease the index value and call the function itself(go to step 2).

The function calls itself repeatedly until it finds the last occurrence of the character.

3) If the return value is >-1 then print the return value which indicates the last occurrence of the character in the string.

Output:

x

Check Also

C Program To Find Reverse Of An Array – C Programs

C program to find the reverse of an array – In this article, we will ...