R
Radhika Sambamurti
Hi,
I've written a substring function.
The prototype is: int substr(char s1[], char s2[])
Returns 1 if s2 is a substring of s1, else it returns 0.
I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function.
//// code follows ///////////
//implement a substring function, where if string2 is a sub of string 1, then the value returned is 1 else, 0
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 10;
int substring(char *str1, char *str2); //prototype
int main()
{
char response = 'y';
char str1[MAX];
char str2[MAX];
int result = 0;
while(response == 'Y' || response == 'y')
{
cout << "Please enter string 1: " ;
cin >> str1;
cout << "\nPlease enter string 2: ";
cin >> str2;
cout << endl;
result = substring(str1, str2); //function call
if(result == 1)
cout << "string " << str2 << " is a substring of " << str1 << endl;
else
cout << "string " << str2 << " is NOT a substring of " << str1 << endl;
cout << "would you like to enter another? (Y/N) " << endl;
cin >> response;
}
return 0;
}
//''''''''''''''''''''''''''''''''''''''''''
//Function definition substring
int substring(char *str1, char *str2)
{
int len1, len2, i=0, j = 0, k, n;
len1=strlen(str1); //determine the lengths of the strings
len2=strlen(str2);
while(i <len1)
{
while( str2[j] == str1 )
{
for(k=1, n=i+1; k<len2; k++, n++)
//n is assigned i, which is where the match //starts in string 1- starting at the next letter
{
if( str2[k] != str1[n] )
return 0; //the match fails after having made an initial match
}
return 1;
j++;
}
i++;
}
}
I've written a substring function.
The prototype is: int substr(char s1[], char s2[])
Returns 1 if s2 is a substring of s1, else it returns 0.
I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function.
//// code follows ///////////
//implement a substring function, where if string2 is a sub of string 1, then the value returned is 1 else, 0
#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 10;
int substring(char *str1, char *str2); //prototype
int main()
{
char response = 'y';
char str1[MAX];
char str2[MAX];
int result = 0;
while(response == 'Y' || response == 'y')
{
cout << "Please enter string 1: " ;
cin >> str1;
cout << "\nPlease enter string 2: ";
cin >> str2;
cout << endl;
result = substring(str1, str2); //function call
if(result == 1)
cout << "string " << str2 << " is a substring of " << str1 << endl;
else
cout << "string " << str2 << " is NOT a substring of " << str1 << endl;
cout << "would you like to enter another? (Y/N) " << endl;
cin >> response;
}
return 0;
}
//''''''''''''''''''''''''''''''''''''''''''
//Function definition substring
int substring(char *str1, char *str2)
{
int len1, len2, i=0, j = 0, k, n;
len1=strlen(str1); //determine the lengths of the strings
len2=strlen(str2);
while(i <len1)
{
while( str2[j] == str1 )
{
for(k=1, n=i+1; k<len2; k++, n++)
//n is assigned i, which is where the match //starts in string 1- starting at the next letter
{
if( str2[k] != str1[n] )
return 0; //the match fails after having made an initial match
}
return 1;
j++;
}
i++;
}
}