Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// Warning: this program uses gets(), which is unsafe. // I WANT TO REPLACE GETS

ID: 3701943 • Letter: #

Question

// Warning: this program uses gets(), which is unsafe.

// I WANT TO REPLACE GETS BECAUSE IT IS UNSAFE

// I NEED HELP PLEASE

#include<iostream>

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 != '')

  

{

  

char *temp = charSet; //take a temporary pointer to charSet

  

//compare each char of source with all chars of temp

  

while(*temp != '')

  

{

  

//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

  

}

Explanation / Answer

Please find my fixed code.

I have used fgets() that best.

#include<iostream>

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

      

        fgets(s1, 50, stdin); //store it in s1

      

        cout<< "Please enter Char set string: " << endl; // ask user to enter charset string

      

        fgets(s2, 50, stdin); //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 != '')

      

    {

      

        char *temp = charSet; //take a temporary pointer to charSet

      

        //compare each char of source with all chars of temp

      

        while(*temp != '')

          

        {

          

            //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

  

}

Please rate my answer if it helped you!!