write a own function for strstr



write a own function for strstr..

Answer / abdur rab

#include <stdio.h>

char* str_str ( char* cp_str, char* cp_pattern )
{
char* cp_temp = NULL;
int n_pattern_length = 0;

n_pattern_length = strlen ( cp_pattern );

while ( cp_str && *cp_str ) {
if ( !strncmp ( cp_str, cp_pattern, (
n_pattern_length ) ) ) {
cp_temp = cp_str;
break;
}
else cp_str++;
}

return ( cp_temp );

}

int main ( int argc, char* argv [ ] )
{
char array [] = {"Hello World"};
char* cp_temp = NULL;

cp_temp = str_str ( array, "lo " );
if ( NULL != cp_temp ) {
printf ("\n%s", cp_temp);
} else printf ("\nReturned null");

return ( 0 );

}

Is This Answer Correct ?    12 Yes 7 No

Post New Answer

More C Interview Questions

Write a code to generate a series where the next element is the sum of last k terms.

0 Answers   Aspiring Minds,


What is #define?

0 Answers  


What are actual arguments?

0 Answers  


What are the advantages of using new operator as compared to the function malloc ()?

0 Answers   NIIT,


Write a programe print the sum of series 0,1,2,.....10

7 Answers  


please give code for this 1 2 4 7 11 16

11 Answers   Intel, Wipro,


Explain what is dynamic data structure?

0 Answers  


If jack lies on Mon, Tue Wed and jill lies on Thursday, Friday and Saturday. If both together tell they lied yesterday. So c the given options and then c cos in the given dates one will be saying the truth and one will be lying. I got Thursday as option because jack is saying the truth he lied yest but jill is lying again as he lies on that day.

0 Answers   TCS,


How many ways are there to swap two numbers without using temporary variable? Give the each logic.

9 Answers  


Is fortran still used in 2018?

0 Answers  


Explain how can I right-justify a string?

0 Answers  


please give me answer with details #include<stdio.h> main() { int i=1; i=(++i)*(++i)*(++i); printf("%d",i); getch(); }

3 Answers  


Categories