In this tutorial you will learn about the C Program to Find Last Occurrence of a Character in a String and its application with practical example.
C Program to Find Last Occurrence of a Character in a String
In this tutorial, we will learn to create a C program that will Find the Last Occurrence of a Character in a String in C programming
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
What is a string?
As we all know the String is a collection of characters. In strings, only one variable is declared which can store multiple values. The String can consist of all the typeable data it means Digits, Alphabets, Symbols, etc. In this program, we will use strings.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1. Declaring the variables for the program. 2. Taking the input string from the user. 3. Taking the input from user the character needs to be searched. 4. Passing those variables to forloop. 5. Using conditional statements for the program to find the last occerence of character. 6. Printing the result position. 7. End the program. |
Find Last Occurrence of a Character in a String
In this program first, we will take input string from the user. Then we will take the character to be searched from the user. Then will Find the last occurrence of that character in that string. Printing the position of that Character.
With the help of this program, we can Find the Last Occurrence of a Character in a String.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/* C Program to find Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> int main() { //declaring the required variables for the program. char strg[100], ch; int i, indx; indx = -1; //taking input string from the user for the program. printf("\n Please Enter the String for the program : "); gets(strg); //taking the character to be searched in a string. printf("\n Please Enter the Character that you want to Search for : "); scanf("%c", &ch); //Finding the character in the string. for(i = 0; i <= strlen(strg); i++) { if(strg[i] == ch) { indx = i; } } if(indx == -1) { //Printing the result from the string printf("\n Sorry!! We haven't found the the Search Element '%c' ", ch); } else { //Printing the result from the string printf("\n The Last Occurrence of the Search Element '%c'' at Position %d ", ch, indx + 1); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- strg[100] = it will hold the string value.
- ch = it will hold the character value.
- indx = it will hold the integer value.
- i = it will hold the integer value.
Input string from the user for the program and Take the input character from the user to search.
Searching the Character in the string.
Printing output position of the string.