i have this code this code checks the string t in string s and tells that string t contained in string s or not and if not then i have to remove the first character and then again compare it anyone can tell me how i can again implement it
#include <iostream>
#include <string>
usingnamespace std;
void find(char str1[], char str2[])
{
/* Perform the search. */
char *ptr = "";
ptr = strstr(str1, str2);
if ( ptr == NULL )
cout << "No match was found.\n";
else
cout << str2 << " contained in " << str1;
}
int main()
{
char *ptr, str1[80], str2[80];
cout << "Enter the string to be searched: ";
cin >> str1;
cout << "Enter the target string: ";
cin >> str2;
find(str1, str2);
return 0;
}
these are the statements to remove first character anyone can tell me where and how i can put these statements and how i can again compare the strings after removing first character. please tell me
You need the erasing code after line 12. If you need to repeat until str2 is found, wrap lines 9-14 in a while loop.
You could make it a while(true) loop, and add a break if ptr != 0, or make it a while(ptr == 0) and initially set ptr to 0, or make it a do ... while(ptr == 0) loop.
The erasing code you have there is for std::string. If you have to use it, you'll need to change your whole program (just a little). If you don't, you can erase from a c-string as well. Write a for loop that does str[i] = str[i+1] for all characters in the string.
random notes: line 8. no need to initialize to "". You don't use it anywhere. It is common to initialize pointers to 0, for safety. You can also leave it uninitialized.
line 20. You don't use that ptr anywhere. It's not is any way related to the ptr in find().