Home > Cpp > C++ Program to implement File Handling by using ifstream & ofstream (Prg-2) – Q30

C++ Program to implement File Handling by using ifstream & ofstream (Prg-2) – Q30

January 30, 2010 Leave a comment Go to comments

Q30. Program to implement Function Overloading:

Write a paragraph (Atleast 2 lines of sentences) in a file. Read the same file in three different ways –
  i) Reverse the whole paragraph.
 ii) Line by Line Reverse the whole para.
iii) Break the pragraph int two equal halves. Reverse the second half of the pargaraph.

… from College notes (BCA/MCA assignments):

#include <string.h>
#include <fstream.h>
#include <conio.h>

void main(){
	char para[500], ln[80];
	int len, i, half;
	clrscr();

	cout<<"\n Enter a Paragraph (Quit by ^Z):-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	cin.getline(para, 500, '^Z');
	len = strlen(para);

	ofstream fout;
	fout.open("para.txt", ios::out);
	fout.write((char *) &para, len);
	fout.close();

	ifstream fin;
	cout<<"\n Reverse the Whole Paragraph:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	fin.open("para.txt", ios::in);
	fin.read((char *) &para, sizeof(para));
	len = strlen(para);
	for(i=len; i>=0; i--)
		cout<<para[i];
	fin.close();
	getch();

	cout<<"\n Line by line Reverse the Whole Paragraph:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	fin.open("para.txt", ios::in);
	while(fin){
		fin.getline(ln, 80);
		len = strlen(ln);
		for(i=len; i>=0; i--)
			cout<<ln[i];
		cout<<endl;
		}
	fin.close();
	getch();

	cout<<"\n Breaking The Para Into 2 halves and Reversing the 2nd Para:-";
	cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	fin.open("para.txt", ios::in);
	fin.read((char *) &para, sizeof(para));
	len = strlen(para);
	half = len / 2;
	for(i=0; i<=half; i++)
		cout<<para[i];
	for(i=len; i>half; i--)
		cout<<para[i];
	fin.close();
	getch();
	}

 

Output:

Enter a Paragraph (Quit by ^Z):-

My name is Manoj Pandey.
I study in APEEJAY Institute.
I’m Doing MCA from there.
I live in Dwarka Sec. 6.
^Z

Reverse the Whole Paragraph:-

.6 .ceS akrawD ni evil I
.ereht morf ACM gnioD m’I
.etutitsnI YAJEEPA ni yduts I
.yednaP jonaM si eman yM

Line by line Reverse the Whole Paragraph:-

.yednaP jonaM si eman yM
.etutitsnI YAJEEPA ni yduts I
.ereht morf ACM gnioD m’I
.6 .ceS akrawD ni evil I

Breaking The Para Into 2 halves and Reversing the 2nd Para:-

My name is Manoj Pandey.
I study in APEEJAY Institute.
.6 .ceS akrawD ni evil I
.ereht morf ACM gnioD m’I


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: