Write a program in C++ returning starting locations of a
substring using pointers

Answer Posted / pramod

#include<stdio.h>
#include<iostream.h>

int main()
{
char* mystrstr(char*,char*);
char str1[20];
char str2[10];
cout<<"\n Enter two strings\t";
cin>>str1>>str2;
cout<<"\nstr1 = "<<str1<<" str2 "<<str2 ;
char* c= mystrstr(str1,str2);
if(c!=NULL)
printf("\nc = %s\n",c);
return 0;
}

char* mystrstr(char* str1, char* str2)
{
char *cp = (char *) str1;
char *s1, *s2;

if ( !*str2 )
return((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )
{
s1++;
s2++;
}

if (!*s2)
{
printf("\n string found\n");
return(cp);
}
cp++;
}
return(NULL);
}

Is This Answer Correct ?    7 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is stl stack?

666


Who wrote stl?

683


Explain stl.

908


What are stl algorithms?

630


Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.

2098






How connect plc and pc through software

1926


What are the various types of stl containers?

740


what is template and type convertion

2000


In what scenario does the Logical file and Physical file being used?

2329


how can u do connectivity in c++ language? plz send me connectivity code in c++ ?

1899


Describe the My Computer and My Documents folders; identify the elements that are present in every Window.

1860


Is stl part of c++ standard?

647


What does stl stand for in basketball?

650


Describe how to safeguard a system through acquisition of an antivirus Program and systematic backup.

1649


Q1. A. What is unary operator? List out the different operators involved in the unary operator. B. What is an adjust field format flag? Q2. A. Distinguish between a # include and #define. B. Can a list of string be stored within a two dimensional array? Q3. A.Explain how a pointer to function can be declared in C++? B.List the merits and demerits of declaring a nested class in C++? Q4. A. What are the syntactic rules to be avoid ambiguity in multiple inheritence? B. Explain the operation of overloading of an assignment operator. Q5. A. Explain how the virtual base class is different from the conventional base classes of the opps. B. Explain how an exception handler is defined and invoked in a Program. Q6. A. What is a binary file? List the merits and demerits of the binary file usagein C++. B. Write short notes on Text Manipulation Routines. C. Write bites in Turbo c++ Header (“Include”) Files.

2316