#includeusing namespace std;//Input to the function::
//source-source string where search will be performed
//string2search-string to be searched in source
//
//Return from the function::
//The pointer in the source if string2search is present
//Zero on failure to search
//
char* str_str (register char *source, register char *string2search)
{
//temporary variable
register char *sourceptr;
register char *searchptr; //If the string to be search is empty/NULL then return the source string
if (*string2search==’\0′)
return source; //Run a for loop till end of the source string char
for(;*source!=’\0′;source++)
{
//Update the pointer to the source each time
sourceptr = source; //Put the pointer to the first element of the string2search
searchptr = string2search; //Second loop for first element of string2search with
//Updated position of source
//Search ends either on end of string2search (*searchptr!=’\0′)
//or
//Comparison failure (*sourceptr == *searchptr)
//If it ends with searchptr as ‘\0′ then its a success of function
//Else
//Compare again from next pointer of the source with string2search
for(;*searchptr!=’\0′, *sourceptr == *searchptr;*sourceptr++, *searchptr++)
{
} //If all the character of string2search matches
if(*searchptr==’\0′)
return source;
}
return 0;
}int main()
{
cout< cout< return 0;
}
Method2)
#includeusing namespace std;//Input to the function::
//source-source string where search will be performed
//string2search-string to be searched in source
//
//Return from the function::
//The pointer in the source if string2search is present
//Zero on failure to search
//
char* str_str (register char *source, register char *string2search)
{
//temporary variable
register char *sourceptr;
register char *searchptr; //If the string to be search is empty/NULL then return the source string
if (*string2search==’\0′)
return source; //Run a for loop till end of the source string char
for(;*source!=’\0′;source++)
{
//Update the pointer to the source each time
sourceptr = source; //Put the pointer to the first element of the string2search
searchptr = string2search; //Second loop for first element of string2search with
//Updated position of source
//Search ends either on end of string2search (*searchptr!=’\0′)
//or
//Comparison failure (*sourceptr == *searchptr)
//If it ends with searchptr as ‘\0′ then its a success of function
//Else
//Compare again from next pointer of the source with string2search
for(;*searchptr!=’\0′, *sourceptr == *searchptr;*sourceptr++, *searchptr++)
{
} //If all the character of string2search matches
if(*searchptr==’\0′)
return source;
}
return 0;
}int main()
{
cout< cout< return 0;
}
You can follow any responses to this entry through the
RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.