Sie sind auf Seite 1von 15

HOME WORK-4 C++

PART A 1) Write a program which reads a text from the keyboard and displays the following

information on the screen in two columns: a) Numbers of the lines b) Number of words c)Number of characters String should be left-justified and numbers should be right-justified in a suitable field width. Ans 1 #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { clrscr(); int word,lines,charac; char s,newl; char str[100]; word=lines=charac=0; s=' ';newl='\n'; cout<<"Enter String : "; gets(str); for(int i=0;i<=strlen(str);i++) { if(s==' ') word++; else if(newl=='\n') lines++; else charac++; } cout<<"No Of Words in String is "<<word<<endl; cout<<"No Of Lines in String is "<<lines<<endl; cout<<"No Of Characters in String is "<<charac; getch(); }

Write a program to display the MARKS LIST of the 10th class in the same format. Ans 2: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h>
2)

void main() { int eng,pun,math,sci,sst,hindi; clrscr(); cout<<"Enter Marks Of Punjabi : "; cin>>pun; cout<<"Enter Marks Of English : "; cin>>eng; cout<<"Enter Marks Of Hindi : "; cin>>hindi; cout<<"Enter Marks Of Mathemetics : "; cin>>math; cout<<"Enter Marks Of Science : "; cin>>sci; cout<<"Enter Marks Of Social Studies : "; cin>>sst; clrscr(); cout<<"\nStudent Name Is Amit"; cout<<"\nStudent Roll Number Is 10901242"; cout<<"\n-------------------------------------------------"; cout<<"\n|\tEnter Marks Of Punjabi : \t"<<pun<<"\t|"; cout<<"\n|\tEnter Marks Of English : \t"<<eng<<"\t|"; cout<<"\n|\tEnter Marks Of Hindi : \t\t"<<hindi<<"\t|"; cout<<"\n|\tEnter Marks Of Mathemetics : \t"<<math<<"\t|"; cout<<"\n|\tEnter Marks Of Science :

\t"<<sci<<"\t|"; cout<<"\n|\tEnter Marks Of Social Studies : "<<sst<<"\t|"; cout<<"\n-------------------------------------------------"; getch(); } 3) Write a program to create a file containing the name and rollno of the student. Write operations to (1) add new rollno and name.(2) delete the rollno input (3) input the rollno and display name. Ans 3: #include<iostream.h> #include<iomanip.h> #include<process.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<fstream.h> class record { char name[25]; int roll; public: void getData() { cout<<"Enter Roll No : "; cin>>roll; cout<<"Enter Name : ";

gets(name); } void Search() { cout<<"Enter Roll Number : "; cin>>roll; } void putData() { cout<<setw(10)<<name<<setw(10) <<name<<endl; } }; void main() { record rec; fstream file; char name[25]; int roll,ch; for(;;) { clrscr(); cout<<"\nPress 1 For Add New Record"; cout<<"\nPress 2

For Delete Record"; cout<<"\nPress 3 For Display Name"; cout<<"\nPress 4 For Exit"; cout<<"\nEnter Ur Choice : "; cin>>ch; switch(ch) { case 1: rec.getData(); char ch; cin.get(ch); file.write((char *)&file,sizeof file); file.seekg(0); while(file.read((char *) &file,sizeof file))

rec.putData(); file.close(); break; case 2:

fstream file; file.open ("Record.xls",ios::in|ios::out); file.seekg(0,ios::beg); for(int i=0;i<file.eof();i++) file.put(roll);

while(file)

file.get(ch);

cout<<name;

break;

case 3:

rec.Search(); fstream file; file.open ("Record.xls",ios::in|ios::out); file.seekg(0,ios::beg); for(int i=0;i<file.eof();i++) file.put(roll);

while(file) { file.get(ch); cout<<name;

} break; case 4: exit(1); break;

default:

cout<<"Invalid Choise"; break; } } PART B 4) Write a program to copy the contents of the one file to another. Ans 4
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<fstream.h> void main() { fstream file1,file2; char ch; file1.open("file1.doc",ios::in); if(file1==NULL) { cout<<"canot open the siurce file\n";

exit(0); } file2.open("file2.doc",ios::out); if(ft==NULL) cout<<"\ncannot open target file\n"; file1.close(); exit(0); } while(1) { ch=file1.get(); if(ch==EOF) break; else file2.writeline(ch,file1); } file1.close(); file2.close(); getch(); }

5) Write the program to store the database of the student in the file and then delete the specific record from the database?
Ans 5

#include<iostream.h> #include<iomanip.h> #include<process.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<fstream.h> class record { char name[25]; int roll; int marks,Per; public:

void getData() { cout<<"Enter cin>>roll; cout<<"Enter gets(name); cout<<"Enter cin>>marks; cout<<"Enter cin>>Per; } void Search() { cout<<"Enter Roll Number : "; cin>>roll; } void putData() {

Roll No : "; Name : "; Marks : "; Percentage : ";

cout<<setw(10)<<name<< setw(10) <<name<< setw(10) <<marks<< setw(10) <<Per<<endl; } }; void main() { record rec;

fstream file; char name[25]; int roll,ch; for(;;) { clrscr(); cout<<"\nPress 1 For Add New Record"; cout<<"\nPress 2 For Delete Record"; cout<<"\nPress 4 For Exit"; cout<<"\nEnter Ur Choise : "; cin>>ch; switch(ch) { case 1: rec.getData(); char ch; cin.get(ch); file.write((char *)&file,sizeof

file); file.seekg(0); while(file.read((char *) &file,sizeof file)) rec.putData(); file.close(); break; case 2:

fstream file; file.open ("Record.xls",ios::in|ios::out); file.seekg(0,ios::beg); for(int i=0;i<file.eof();i++) file.put(roll);

while(file) { file.get(ch); cout<<name;

break;

case 3:

exit(1); break; default: cout<<"Invalid Choise"; break; } }

6) Given a text file containing some text. Write the program that prompts the user to input name text file, reads this file and replaces each occurrences of character x with uppercase X. Ans 6:

#include<iostream.h> #include<iomanip.h> #include<process.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<fstream.h> void main() { clrscr(); ofstream out("data.doc"); ifstream in("data.doc"); char ch[25]; cout<<"Enter String : "; gets(ch); out<<ch; char sre; in.open("data.doc",ios::in); sre='x'; if(in.eof()) { for(int i=0;i<strlen(ch);i++) if(sre==ch[i]) ch[i]='X'; } getch(); }

Das könnte Ihnen auch gefallen