strrchr(): searching for matching character

We can search and get the pointer of last occurance of mathching character by using strrchr() string function.
strrchr(main_string, search_string);
Output is pointer of last occurrence of matching character. Returns null if searched character is not found.
#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p;
   p = strrchr(str,'e');
   printf("Position :%d",p-str+1);
   return 0;
}
The output of above code is here
Position :18
In above code, by subtracting the pointer of main character and by adding 1 to it , we will get the position of matching character related to main string.

As we get a pointer by using strrchr() , so we can display the character value so rest of the string starting from searched character will be returned.
#include <stdio.h>
#include <string.h>
int main(void){
   char str[100]="Welcome to plus2net";
   char *p;
   p = strrchr(str,'e');
   printf("Position :%d , %s",p-str+1,p);
   return 0;
}
Output is here
Position :18, et
Your Rating




Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer