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

#include<stdio.h> void main() { int a [5]; for (i=0; i<=4; i++) printf(“%d” ,a[i]); }

8 Answers  


What is a memory leak in structures? How can we rectify that?

2 Answers  


What is the use of c language in real life?

0 Answers  


Why can arithmetic operations not be performed on void pointers?

0 Answers  


How can I send mail from within a c program?

0 Answers  


how logic is used

0 Answers  


Given an array of characters, how would you reverse it? How would you reverse it without using indexing in the array?

1 Answers   Microsoft,


write a program to copy the string using switch case?

0 Answers   Mind Tree,


Write a c code segment using a for loop that calculates and prints the sum of the even integers from 2 to 30, inclusive?

4 Answers  


#include<stdio.h> main() {int i=1;j=1; for(;;) {if(i>5) break; else j+=1; printf("\n%d",j) i+=j; } }

7 Answers   HCL,


What are the uses of pre-processor directives?

2 Answers  


Differentiate between functions getch() and getche().

0 Answers  


Categories