Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Function strrchr in C programming

September 16, 2013 Posted by: GPA Views: 634 0 comments

Problem

Write simple example using strrchr() function in C programming.

Solution

In this section, you will get the explanation about strrchr() function of header file <string.h>.

Declaration:

char *strrchr( const char *str, int character );

This function finds for the last occurrence of the input character in a string. If the character is found, it returns a pointer to the character in the string; otherwise, it returns NULL.

In this example we prompt the user to enter string and character for searching.  

Example:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>//include this header file for function strrchr
int main(int argc, char *argv[])
{
char inputstring[20];//array of character
char sCharacter;//variable for searching char
printf("globalproganswer.com\n")
printf("Enter string: ");//promt user to enter some string
gets(inputstring);//read string from keyboard
printf("What character do you want to search: ");//promt user to enter some character
sCharacter=getchar();
//call function strrchr
if (strrchr(inputstring, sCharacter ) != NULL ){
//show result found
printf("The remainder of string beginning\nwith the last occurrence of character \'%c\' is: %s", sCharacter,strrchr(inputstring, sCharacter ) );
} else {
//show result not found
printf( "\'%c\' was not found in \"%s\".\n",sCharacter,inputstring);
}

printf("\n");//new line
system("PAUSE");//delay
return 0;
}

Output

Function strrchr

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe