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 ?    8 Yes 9 No

Post New Answer

More STL Interview Questions

what's the difference between abstract class and concreate class? what's the meaning of standard template library(STL)?

2 Answers  


To modify an, existing worksheet. What steps are involved for: 1. Inserting and deleting rows and columns. 2. Printing cell formulas 3Jld displayed values 3. Using the page setup command

0 Answers  


what is compiler?

4 Answers   NASA,


what is template and type convertion

0 Answers   TATA,


what's the difference between abstract class and concreate class? what's the meaning of standard template library(STL)?

6 Answers  






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

0 Answers  


how to making game in c++ ?

0 Answers  


What is C++ could you enplane me please?

1 Answers  


What is the STL?

2 Answers   Epson, HP,


What is stl stack?

0 Answers  


What is the Difference between CArray and CList?

1 Answers   ProdEx Technologies, Siemens,


How does an stl file work?

0 Answers  


Categories