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 strstr in C programming

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

Problem

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

Solution

In this section, you will know about strstr() function of header file <string.h>.

Declaration:

char *strstr( const char * firstString , const char * secondString);

This function finds for the first occurrence of its second string parameter in its first string parameter. If the secondString is found in the firstString, a pointer to the location of the string in the first parameter is returned.

In this example we prompt the user to enter two strings.

Example:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>//for function strstr
//main function
int main(int argc, char *argv[])
{
char firstString[50];//variable for first String
char secondString[50];//variable for second String
printf("globalproganswer.com\n");
printf("Enter first string: ");//promt user to enter some string
gets(firstString);//read string from keyboard
printf("Enter second string: ");//promt user adto enter some string
gets(secondString);//read string from keyboard
char* strResult =strstr(firstString, secondString);
if(strResult!=NULL){//check is result not equal NULL then print
printf("The remainder of fisrt string beginning with the \nfirst occurrence of second string is: \'%s\'",strResult);
}else{
printf("No any character");
}
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