Question
// Warning: this program uses gets(), which is unsafe. // I WANT TO REPLACE GETS BECAUSE IT IS UNSAFE // I NEED HELP PLEASE #include
// Warning: this program uses gets(), which is unsafe.
// I WANT TO REPLACE GETS BECAUSE IT IS UNSAFE
// I NEED HELP PLEASE
#include
using namespace std;
//searcher prototype
char *searcher (char *source, char *charSet);
int main()
{
char s1[50], s2[50]; //to hold input strings
char choice; //to store user choice y or n
while(true)
{
cout << "Please enter source string: " << endl; //ask user to enter source string
gets(s1); //store it in s1
cout<< "Please enter Char set string: " << endl; // ask user to enter charset string
gets(s2); //store it in s2
cout << "The Source String " << s1 << endl; //print s1
cout << "The Char set string " << s2 << endl; //print s2
cout << "The Address of source string " << &s1 << endl; //print address of s1
//call searcher function and store returned pointer in c
char *c = searcher(s1,s2);
//if c is not null then print sub string
if(c != NULL)
{
cout << "The Substring found by searcher " << c << endl;
}
//if c is null print unsuccessful search
else
cout << " Unsuccessful search " << endl;
cout << endl;
cout << " Do you want to continue? y or n " << endl; //ask user to continue or not
cin >> choice; //get input
//if choice is n or N terminate program
if(choice=='n'||choice=='N')
break;
cin.ignore();
}
return 0;
}
//searcher function
char *searcher (char *source, char *charSet)
{
//for each character in source till end of string
while(*source != '\0')
{
char *temp = charSet; //take a temporary pointer to charSet
//compare each char of source with all chars of temp
while(*temp != '\0')
{
//if any match found return pointer to that character
if(*temp == *source)
return source;
temp++; //move to next char
}
source++; //move to next char in source
}
return NULL; //if no match found return NULL
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started