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

Write a C++ Program that uses only: 1. Uses strings 2. Passes strings to functio

ID: 3920116 • Letter: W

Question

Write a C++ Program that uses only:

1. Uses strings

2. Passes strings to functions

2. Uses file I/O.


Project Requirements:

1. Write a function that, given two strings as input, checks if one string is a substring of the other. If so, capitalize the substring and print the new capitalized string.

For example: If the input is ‘Begin’ and ‘in’. Then we must check if any of these strings is a substring of the other. If yes print a statement as below.

Yes, ‘in’ is substring of ‘Begin’

Now capitalize the substring and write the new capitalized string to a new file specified by the user. The new string for the above example is ‘BegIN’. You should ignore case when comparing strings; that is, given ‘Hilly’ and ‘hill’ as input the function should still find hill to be a substring of Hilly.

2. Read the words (strings) from a file the user chooses in main() and write the new strings to another file of the user’s choice.

3. You have a few choice in implementation:

1. Use of either old style c-strings (character arrays) or the string (#include <string>) class.

2. Use of a data structure (array or vector) to store the strings. You can store the strings in such a container, or you can just read and write them without using

EXPECTED OUTPUT:

Please enter the input filename: input.txt

Please enter the output filename: output.txt

The original strings are:

String

Ring

Ring is a substring of String

The new string is: “StRING”

Content has been written to the file output.txt.

Explanation / Answer

#include<iostream>

#include<fstream>

#include<string.h>

#include <algorithm>

using namespace std;

//method to find substring.

void find_subString(string s1,string s2,char *out)

{

string r=s1,p=s2;

transform(r.begin(),r.end(), r.begin(), ::toupper);

transform(p.begin(),p.end(), p.begin(), ::toupper);

if(r.find(p) != string::npos)

{

//means found...

//so//

//making s2 to upper

cout<<s2<<" is a substring of "<<s1<<endl;

int i,j;

for(i = 0, j = 0; s1[i] != '' && s2[j] != ''; i++)

{

if(r[i] == p[j])

{

j++;

}

else

{

j = 0;

}

}

int pos= i-j;

transform(s1.begin()+pos,s1.end(), s1.begin()+pos, ::toupper);

cout<<"The new String:"<<s1<<endl;

//writing to file..

fstream file;

file.open(out);

file<<s2;//writing to file

file.close();

}

else

{

cout<<"Not a Substring ";

}

}

int main()

{

char infile[100],outfile[100];

cout<<"Enter input file name:";

cin>>infile;

cout<<"Enter output file name:";

cin>>outfile;

//reading file..

fstream file;

file.open(infile);

string s1,s2;

//reading strings;

file>>s1;

file>>s2;

file.close();

cout<<" Strings readed from file :"<<s1<<" "<<s2<<endl;

find_subString(s1,s2,outfile);

return 0;

}

output:

Enter input file name:input.txt
Enter output file name:output.txt

Strings readed from file :String Ring
Ring is a substring of String
The new String:StRING


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote