write a own function for strstr

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is masking?

902


Can you explain what keyboard debouncing is, and where and why we us it? please give some examples

1927


Is c still used?

795


What is meant by operator precedence?

879


Why can arithmetic operations not be performed on void pointers?

809


How can I invoke another program or command and trap its output?

854


What is data structure in c language?

833


Explain how can type-insensitive macros be created?

772


1. Write a function to display the sum of two numbers in the following ways: By using (i) pass by value (ii) pass by address a. function with argument and with return value b. function with argument and without return value c. without argument , with return value d. without argument , without return value Note: Use pass by address.

2533


How can I list all of the predefined identifiers?

756


How are 16- and 32-bit numbers stored?

971


What is a stream?

873


how to print the character with maximum occurence and print that number of occurence too in a string given ?

2243


Why is c platform dependent?

800


Is there a way to switch on strings?

819