Write a C++ Program that: Uses strings Passes strings to functions Uses file I/O
ID: 3920026 • Letter: W
Question
Write a C++ Program that:
Uses strings
Passes strings to functions
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:
Use of either old style c-strings (character arrays) or the string (#include <string>) class.
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 storage.
Sample
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)
{
if(s1.find(s2) != string::npos)
{
//means found...
//so//
//making s2 to upper
transform(s2.begin(),s2.end(), s2.begin(), ::toupper);
cout<<"converted string:"<<s2<<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 :begin in
converted string:IN
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.