Write a program in C++ returning starting locations of a substring using pointers?
Submitted by: Administrator#include<stdio.h>
#include<iostream.h>
int main()
{
char* mystrstr(char*,char*);
char str1[20];
char str2[10];
cout<<"n Enter two stringst";
cin>>str1>>str2;
cout<<"nstr1 = "<<str1<<" str2 "<<str2 ;
char* c= mystrstr(str1,str2);
if(c!=NULL)
printf("nc = %sn",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 foundn");
return(cp);
}
cp++;
}
return(NULL);
}
Submitted by: Administrator
#include<iostream.h>
int main()
{
char* mystrstr(char*,char*);
char str1[20];
char str2[10];
cout<<"n Enter two stringst";
cin>>str1>>str2;
cout<<"nstr1 = "<<str1<<" str2 "<<str2 ;
char* c= mystrstr(str1,str2);
if(c!=NULL)
printf("nc = %sn",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 foundn");
return(cp);
}
cp++;
}
return(NULL);
}
Submitted by: Administrator
Read Online STL Job Interview Questions And Answers
Top STL Questions
☺ | Write a program in C/C++ to implement reader- writer problem? |
☺ | Write a program in C++ returning starting locations of a substring using pointers? |
☺ | Why & sign is used in copy constructor? |
☺ | 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? |
☺ | WHAT IS THE DIFFERENCE BETWEEN C++ AND VC++? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |