Week 15 solutions
15a) Write a C++ program that removes all vowels
from a file.For example, once upon a time!
Becomes nc pn tm!.
File: vowel.txt
once upon a time!
Program:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream in("vowel.txt");
ofstream file("15a_op.txt");
char c;
cout<<"\nFile before removing vowels is :\n";
c=in.get();
while(in.good())
{
cout<<c;
if(c!='a'&&c!='e'&&c!='i'&&c!='o'&&c!='u')
file<<c;
c=in.get();
}
in.close();
file.close();
cout<<"\nFile after removing vowels is :\n";
ifstream nh("15a_op.txt");
cout<<nh.rdbuf();
nh.close();
return 0;
}
output:
nc pn tm!.
15 b)Write a C++ program that replaces punctuation with
whitespace.For example,“don?t usethe as-if rule”
becomes dont use the asif rule”.
file:punct.txt
“don't usethe as-if rule”
program:
#include<iostream>
#include<fstream>
#include<cctype> or include<ctype.h>
using namespace std;
int main()
{
ifstream in("punct.txt");
ofstream file("15b_op.txt");
char c;
cout<<"\nFile before removing punctuations is :\n";
c=in.get();
while(in.good())
{
cout<<c;
if(!ispunct(c))
file<<c;
c=in.get();
}
in.close();
file.close();
cout<<"\nFile after removing vowels is :\n";
ifstream nh("15b_op.txt");
cout<<nh.rdbuf();
nh.close();
return 0;
}
output: (15b_op.txt)
dont use the asif rule
15 c) Write a C++ program to reverse the order of characters
in a text file.For example,asdfghjkl becomes lkjhgfdsa.
File:.txt
asdfghjkl
program:
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
vector<char>v;
ifstream in("15c.txt");
cout<<"\nInput File :\n";
char c=in.get();
while(!in.eof())
{
cout<<c;
v.push_back(c);
c=in.get();
}
in.close();
ofstream file("15c_op.txt");
for(int i=v.size()-1;i>=0;i--)
{
file<<v[i];
}
file.close();
cout<<"\nFile after reversing the contents :\n";
ifstream nh("15c_op.txt");
cout<<nh.rdbuf();
nh.close();
return 0;;
}
output: (15c_op.txt)
lkjhgfdsa.
No comments:
Post a Comment