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



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

Answer / 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

More STL Interview Questions

method overloading means what?

2 Answers   CTS,


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

1 Answers  


What is the disadvantage of templates ?

2 Answers   NSN, Symphony,


write a c++ to define a class box with length,breadth and height as data member and input value(),printvalue() and volume() as member functions.

3 Answers  


what is electronic software

1 Answers  






Describe the elements of Microsoft Word screen. Write down steps for creating, saving, retrieving, editing and printing a document.

2 Answers  


How can you create a bulleted list, numbered list and an outline by using bullets and numbering command. Explain with the help of example.

0 Answers  


what is strcture i++ i ++i answer to this i=5 what is the out put

6 Answers  


What is meant by stl in c++?

0 Answers  


Write a program in C/C++ to implement reader- writer problem

1 Answers   Wipro,


What is stl language?

0 Answers  


Assume I have a linked list contains all of the alphabets from "A" to "Z?" I want to find the letter "Q" in the list, how does you perform the search to find the "Q?"

2 Answers  


Categories