The library function used to find the last occurrence of a character in a string is
Question
The library function used to find the last occurrence of a character in a string is
Solution
The library function used to find the last occurrence of a character in a string is strrchr()
in C language. Here is how you can use it:
-
Include the string library in your program using
#include <string.h>
. -
Declare your string and the character you want to find. For example:
char str[] = "Hello, World!"; char ch = 'o';
. -
Use the
strrchr()
function to find the last occurrence of the character. For example:char *res = strrchr(str, ch);
. -
The function will return a pointer to the last occurrence of the character in the string. If the character is not found, the function returns a null pointer.
-
You can print the result using
printf()
. If the result is not null, print the string from the found character to the end. If the result is null, print a message saying the character was not found. For example:
if (res != NULL)
printf("The character '%c' is found and the last occurrence is '%s'\n", ch, res);
else
printf("The character '%c' is not found\n", ch);
This will print: The character 'o' is found and the last occurrence is 'orld!'
.
Similar Questions
Which of the following function is used to find the first occurrence of a given string in another string?
Which of the library function put back the length of a string?a.strlenb.strcatc.strcmpd.strcpy
What is the slice expression that gives every third character of string s, starting with the last character and proceeding backward to the first?
Which method is used to find a substring within a string in C++?find()substr()search()locate()
Which of the following built-in string methods can be used to find the character at a specific index position?length()charAt()main()toUpperCase()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.