You are on page 1of 29

ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

1. Write a C++ program that uses a recursive function for solving Towers of Hanoi problem.

Program:

#include<iostream>
#include<cmath>
using namespace std;
void hanoi(int x,char from,char to,char aux)
{
if(x==1)
cout<<"\n move disk from"<<from<<"to"<<to<<endl;
else
{
hanoi(x-1,from,aux,to);
cout<<"\n move disk from"<<from<<"to"<<to<<endl;
hanoi(x-1,aux,to,from);
}
}
int main()
{
int disk;
int moves;
cout<<"\n enter the number of disks you want to play with:";
cin>>disk;
moves=pow(2,disk)-1;
cout<<"no.of moves required is:"<<moves;
hanoi(disk,'A','C','B');
return 0;
}

Output:

Description:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

Suppose three pegs labeled A,B and c are given and suppose on peg A there are placed a finite
number ‘n’ of disks with decreasing size. For the case n=6, the object of game is to move the disk
from peg A to peg C using peg B as an auxiliary. The rules are:

 Only one disk can be moved at a time. Specifically, only the top disk on any peg may be
moved to any other peg.
 At no time can a larger disk be placed on a smaller disk

2. Write a C++ program to find both the largest and smallest number in a list of integers.

Program:
#include<iostream>
using namespace std;
int main()
{
int a[100],max,min,i,n;
cout<<"Enter how many integers do u want? :";
cin>>n;
cout<<"\n enter the integers";
for( i=0; i< n ; i++)
{
cin>>a[i];
if(i==0)
{
max=a[i];
min=a[i];
}
if(a[i]>max)
max= a[i];
if(a[i]<min)
min= a[i];
}
cout<<"\n Maximum : "<< max;

cout<<"\n Minimum : "<< min;


}

Output:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

Description:

The above program is used for finding the maximum and minimum of a number. Initially the user
will ask how many numbers to want to enter. In this program, for entering a group of numbers used
arrays. The first element in the array is compared with all the elements until the end of the array.if the
element in the array is larger value then it is the maximum else it is minimum.

3. Write a C++ program that uses function templates to solve problems 1 and 2 experiments
Program:

#include<iostream>
#include<cmath>
using namespace std;
template<class T>
void hanoi(int x,T from,T to,T aux)
{
if(x==1)
cout<<"move disk from"<<from<<"to"<<to;
else
{
hanoi(x-1,from,aux,to);
cout<<"move disk from"<<from<<"to"<<to;
hanoi(x-1,aux,to,from);
}
}
int main()
{
int disk;
int moves;
cout<<"\n enter the number of disks you want to play with:";
cin>>disk;
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

moves=pow(2,disk)-1;
cout<<"no.of moves required is:"<<moves;
hanoi(disk,1,2,3);
return 0;
}

Output:

#include <iostream.h>
#include <conio.h>
template <class T>
T findMin(T arr[],int n)
{
int i;
T min;
min=arr[0];
for(i=0;i<n;i++)
{
if(min > arr[i])
min=arr[i];
}
return(min);
}
template <class T>
T findMax(T arr[],int n)
{
int i;
T max;
max=arr[0];
for(i=0;i<n;i++)
{
if(max < arr[i])
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

max=arr[i];
}
return(max);
}
int main()
{
clrscr();
int iarr[5];
cout << "Integer Values \n";
for(int i=0; i<5; i++)
{
cout << "Enter integer value " << i+1 << " : ";
cin >> iarr[i];
}
cout<<"Integer Minimum is : "<<findMin(iarr,5)<<"\n";
cout<<"Integer Maximum is : "<<findMax(iarr,5)<<"\n";
getch();
return 0;
}

Output:

Description:

The above program is for finding largest and smallest of a number by using templates concept. In this
program we have used function template where it behaves like a function except that the template can
have arguments of many different types. The usage of a function template saves space in the source

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

code file in addition to limiting changes to one function description and making the code easier to
read.

4. Write a C++ program to implement the matrix ADT using a class. Use operator overloading for
implementation.

Program:
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;
class matrix
{
protected:
int i,j,a[10][10],b[10][10],c[10][10];
int m1,n1,m2,n2,m3,n3;
public:
virtual void read()=0;
virtual void display()=0;
virtual void sum()=0;
virtual void sub()=0;
virtual void mult()=0;
};
class result:public matrix
{
public:
void read();
void sum();
void sub();
void mult();
void display();
};
void result::read()
{

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

cout<<"\nenter the order of matrix A ";


cin>>m1>>n1;
cout<<"\nenter the elements of matrix A ";
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
cin>>a[i][j];
}
}
cout<<"\nenter the order of matrix B ";
cin>>m2>>n2;
cout<<"\nenter the matrix B ";
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
cin>>b[i][j];
}
}
}
void result::display()
{

for(i=0;i<m3;i++)
{
for(j=0;j<n3;j++)
{
cout.width(3);
cout<<c[i][j];
}
cout<<"\n";
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

}
}
void result::sum()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for addition";
}
else
{
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
m3=m1;
n3=n1;
}
}
void result::sub()
{
if((m1!=m2)||(n1!=n2))
{
cout<<"the order should be same for subtraction ";
}
else
{ m3=m1;
n3=n1;
for(i=0;i<m1;i++)
{
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

for(j=0;j<n1;j++)
{
c[i][j]=a[i][j]-b[i][j];
//cout<<a[i][j];
}
}
}
}
void result::mult(void)
{
if(n1!=m2)
{
cout<<"Invalid order limit ";
}
else
{ m3=m1;
n3=n2;
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
for(int k=0;k<n1;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
}
int main()
{
int ch;
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

class matrix *p;


class result r;
p=&r;
while(1)
{
cout<<"\n1. Addition of matrices ";
cout<<"\n2. Subtraction of matrices ";
cout<<"\n3. Multipication of matrices ";
cout<<"\n4. Exit";
cout<<"\nEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
p->read();
p->sum();
p->display();
break;

case 2:
p->read();
p->sub();
p->display();
break;

case 3:
p->read();
p->mult();
p->display();
break;
case 4: exit(0);
}
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

}
}

Output:

Description:

The above program is used for implementing matrix ADT using class. ADT means abstract data type
where we can hide the background details and can be shown the essential information. In this
program we have used operator overloading concept where we can perform addition, subtraction and
multiplication of a matrix.

5. Write the definition for a class called Rectangle that has floating point data members’ length and
width. The class has the following member functions: void setlength(float) to set the length data
member void setwidth(float) to set the width data member float perimeter() to calculate and return
the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show()
to display the length and width of the rectangle int sameArea(Rectangle) that has one parameter of
type Rectangle. sameArea returns 1 if the two Rectangles have the same area, and returns 0 if they
don't.
1. Write the definitions for each of the above member functions.

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

2. Write main function to create two rectangle objects. Set the length and width of the first rectangle
to 5 and 2.5. Set the length and width of the second rectangle to 5 and 18.9. Display each rectangle
and its area and perimeter.

3. Check whether the two Rectangles have the same area and print a message indicating the result.
Set the length and width of the first rectangle to 15 and 6.3. Display each Rectangle and its area and
perimeter again. Again, check whether the two Rectangles have the same area and print a message
indicating the result

Program:
#include<iostream>
using namespace std;
class Rectangle
{
private:
float length;
float width;
public:
void setlength(float);
void setwidth(float);
float perimeter();
float area();
void show();
int sameArea(Rectangle);
};
void Rectangle::setlength(float len)
{
length = len;
}
void Rectangle::setwidth(float wid)
{
width = wid;
}
float Rectangle::perimeter()
{
return (2 * length + 2 * width);
}
float Rectangle::area()
{

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

return length * width;


}
void Rectangle::show()
{
cout << "Length: " << length << " Width: " << width;
}
int Rectangle::sameArea(Rectangle other)
{
float areaf = length * width;
float areas = other.length * other.width;
if (areaf == areas)
return 1;
return 0;
}
int main()
{
Rectangle first;
Rectangle second;

first.setlength(5);
first.setwidth(2.5);
second.setlength(5);
second.setwidth(18.9);
cout << "First rectangle: ";
first.show();
cout << endl << "Area: " << first.area() << "Perimeter: " << first.perimeter() << endl << endl;
cout << "Second rectangle: ";
second.show();
cout << endl << "Area: " << second.area() << "Perimeter: " << second.perimeter() << endl << endl;
if (first.sameArea(second))
cout << "Rectangles have the same area\n";
else
cout << "Rectangles do not have the same area\n";
first.setlength(15);
first.setwidth(6.3);
cout << "First rectangle: ";
first.show();
cout << endl << "Area: " << first.area() << "Perimeter: "<< first.perimeter() << endl << endl;
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

cout << "Second rectangle: ";


second.show();
cout << endl << "Area: " << second.area() << "Perimeter: "<< second.perimeter() << endl << endl;
if (first.sameArea(second))
cout << "Rectangles have the same area\n";
else
cout << "Rectangles do not have the same area\n";
return 0;
}

Output:

Description:
In the above program created a class with class name as rectangle and declared variables and
functions of a class. With the help of scope resolution operator '::' we can define the definitions of the

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

functions of a class. In the program we have created two objects for rectangle class. And set the
values of length and width of two rectangles. And calculated the area and perimeter of a rectangle.
While executing the sameArea () method, if the area of two rectangles are same then it will display
both rectangles area was same, if not print both rectangles are not same.

6. Create a class called MusicIns to contain three methods string(),wind() and perc(). Each of these
methods should initilialize string array to contain the following
i. Veena, guitear, sitar, sarod and mandolin under string
ii. Flute, clarinet, saxophone, nadaswaram and piccolo under wind
iii. Table, mridangam, bangos, drums and tambour under perc
It should also display the contents of the arrays initialized , create a sub class call TypeIns to contain
a method called get() and show(). The get() methods must display a menu as follows
 String instruments
 Wind instruments
 Percussion instruments

Program:

#include<iostream>
#include<cstring>
using namespace std;
class MusicIns
{
protected:
char instrument[15][15];
public:
void string()
{
strcpy(instrument[0], "Veena");
strcpy(instrument[1], "Guitar");
strcpy(instrument[2], "Sitar");
strcpy(instrument[3], "Sarod");
strcpy(instrument[4], "Mandolin");
}
void wind()
{
strcpy(instrument[0], "Flute");
strcpy(instrument[1], "Clarinet");
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

strcpy(instrument[2], "Sexophone");
strcpy(instrument[3], "Nadhaswaram");
strcpy(instrument[4], "Picoolo");
}
void perc(void)
{
strcpy(instrument[0], "Tabla");
strcpy(instrument[1], "Mridangam");
strcpy(instrument[2], "Bangos");
strcpy(instrument[3], "Drums");
strcpy(instrument[4], "Tambour");
}
void show()
{
for(int i=0; i<5; i++)
{
cout<<instrument[i];
cout<<" ";
}
}
};
class typIns : public MusicIns
{
public:
void get()
{
cout<<"1. String Instrument"<<endl;
cout<<"2. wind Instrument"<<endl;
cout<<"3. Percussion Instrument"<<endl;
}
void show(int choice)
{
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

switch(choice)
{
case 1: string();
MusicIns::show();
break;
case 2: wind();
MusicIns::show();
break;
case 3: perc();
MusicIns::show();
break;
}
}
};
int main()
{
int x;
typIns t;
t.get();
cout<<"Enter the Choice:";
cin>>x;
t.show(x);
return 0;
}
Output:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

Description:

In the above program,created a class called MusicIns and contains variables like instrument which is
a character array. and methods like string(),wind(),percussion() are initialized with an array of string
contain values like Veena, guitear, sitar, sarod and mandolin under string, Flute, clarinet, saxophone,
nadaswaram and piccolo under wind. Table, mridangam, bangos, drums and tambour under perc.
Created a subclass called TypeIns and inherited the properties of base class MusicIns.And the class
contain methods like get () and show ().The get () method is used for displaying the menu of the
different instruments like string, wind and percussion. The show () method will displays the items of
that particular instruments based on user choice.

7. Create a base class called shape. It should contain two methods getCoord (), showCoord () to
accept x and y co ordinates and to display the same respectively.Create a sub class called Rect. It
should contain method to display length and breadth of the rectangle called showCoord (). In main
method, execute the showCoord () of Rect class by applying the dynamic method dispatch concept.

Program:
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;
class Shape
{
public:
int xcoor,ycoor;
Shape()

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

{
cout<<"\nPlease enter x and y coordinates:\t";
xcoor=getCoord();
ycoor=getCoord();
showCoord();
}
int getCoord()
{
int x;
cin>>x;
return x;
}
virtual void showCoord()
{
cout<<"\nThe x and y coordinates are :\t";
cout<<"\nX coordinate="<<xcoor;
cout<<"\nY coordinate="<<ycoor;
}
};
class Rect:public Shape
{
public:
int length,breadth;
Rect ()
{
cout<<"\nplease enter length and breadth of rectangle:\t";
cin>>length;
cin>>breadth;
showCoord();
}
virtual void showCoord()
{
cout<<"\nThe length and breadth of rectangle is:\t";
cout<<"\nLength="<<length;
cout<<"\nBreadth="<<breadth;
}
};

int main()
{
Rect r;
return 0;
}
Output:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

Description:

In the above program, created a class with a class name as shape. It contain methods like getcoord()
which is used for taking the coordinates of x and y and showcoord () is used for displaying the values
of the coordinates. And also created sub class called Rec and it inherited the properties of base class
shape. And the subclass contain variables like length and breadth and with the help of showcoord()
method we can display the length and breadth of a rectangle. Here we have used virtual keyword for
the method showcoord why because the base class also contains the same method showcoord
().without using virtual keyword the values of base class will overwrite the values of derived class.i.e,
Rect.

8. Create a class called car. Initialize the color and body attributes to “blue” and “wagon”. there
should be two constructors one is a default the creates blue wagon the other constructor should take
two argcolor, body and initialize. write method toString() that returns the color and body. Create a
sub class funcar. In sub class there are two constructors to invoke super class constructors resp. Write
a method playCD in sub class that displays the message “Beautiful music fills the passenger
compartment” execute the methods to show the messages
1. Mycar is a blue wagon
2. My father‟s car is red convertible.

Program:
#include<iostream>
#include <cstring>
using namespace std;
class Car
{
public:
char* color;
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

char * body;

Car()
{
char* c="Blue";
char* b="Wagon";
color=c;
body=b;
}
Car(char * color1,char * body1)
{
color=color1;
body=body1;
}
void toString1()
{
cout<<color<<body;
}
};
class FunCar: public Car
{
public:
FunCar()
{
Car();
}
FunCar(char* color1,char* body1)
{
Car(color1,body1);
}
void playCD()
{
cout<<"Beautiful music fills the passenger compartment";
}
};
int main()
{
FunCar mycar;
mycar.playCD();
Car fathercar("red","convertible");
cout<<"\n My car is a ";
mycar.toString1();
cout<<"\n My father's car is ";
fathercar.toString1();
return 0;
}
Output:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

Description:

In the above program, created a class called car and it contains attributes like color and body.color
was initialized to blue and body to wagon. Created two constructors, one is a default constructor with
no arguments. And other constructor take two arguments like color1 and body1 and initialized with
values. A method tostring () is used to return the values of color and body. Created a subclass called
Funcar where the two constructors in the baseclass are invoked. And within the subclass a method
called playcd which is used for displaying the message “Beautiful music fills the passenger
compartment”. create an object mycar for Funcar class and execute the methods to show the
messages.

9. Create the ZooAnimal constructor function. The function has 4 parameters – a character string
followed by three integer parameters. In the constructor function dynamically allocate the name field
(20 characters), copy the character string parameter into the name field, and then assign the three
integer parameters to cageNumber, weightDate, and weight respectively.

Program:
#include<iostream>
#include<cstring>
using namespace std;

class zooAnimal
{
char *name;
int weighdate,weight,cageno;
public:

zooAnimal(char *n,int wd,int w,int c)


{
name=new char[20];
strcpy(name,n);
weighdate=wd;

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

weight=w;
cageno=c;
}
void print_det()
{
cout<<"\n Animal Name:"<<name<<endl;
cout<<"Animal Weight Date:"<<weighdate<<endl;
cout<<"Animal Weight:"<<weight<<endl;
cout<<"Animal Cage No:"<<cageno;
}
};
int main()
{
int n,i,aweight,awd,cgno;
char *aname;
zooAnimal *a[10];
clrscr();
cout<<"\n Enter No. of Animals:";
cin>>n;
i=1;
while(i<=n)
{
cout<<"\n Enter Animal "<<i<<" Name:";
cin>>aname;
cout<<"\n Enter Animal Weight:";
cin>>aweight;
cout<<"\n Enter Animal WeighDate:";
cin>>awd;
cout<<"\n Enter Cage No:";
cin>>cgno;
a[i]=new zooAnimal(aname,awd,aweight,cgno);
i++;
}
for(i=1;i<=n;i++)
a[i]->print_det();

return 0;
}

Output:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

10. Write a C++ program to perform operations on complex numbers using operator overloading.

Program:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

class complex
{
int i,r;
public:
void read()
{
cout<<"\nEnter Real Part:";
cin>>r;
cout<<"Enter Imaginary Part:";
cin>>i;
}
void display()
{
cout<<"\n= "<<r<<"+"<<i<<"i";
}
complex operator+(complex a2)
{
complex a;
a.r=r+a2.r;
a.i=i+a2.i;
return a;
}
complex operator-(complex a2)
{
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

complex a;
a.r=r-a2.r;
a.i=i-a2.i;
return a;
}
complex operator*(complex a2)
{
complex a;
a.r=(r*a2.r)-(i*a2.i);
a.i=(r*a2.i)+(i*a2.r);
return a;
}
complex operator/(complex a2)
{
complex a;
a.r=((r*a2.r)+(i*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
a.i=((i*a2.r)-(r*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
return a;
}
};
int main()
{
int ch;
complex a,b,c;
do
{
cout<<"\n1.Addition 2.Substraction";
cout<<" 3.Mulitplication 4..Exit\n";
cout<<"\nEnter the choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a+b;
c.display();
break;
case 2:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=b-a;
c.display();
break;
case 3:
cout<<"\nEnter The First Complex Number:";
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a*b;
c.display();
break;
}
}while(ch!=4);
}

Output:

Description:
The above program is used to perform operations like +,-* on complex numbers using operator
overloading concept. Overloaded operators are functions with special names the keyword operator
followed by the symbol for the operator being defined. Like any other function, an overloaded
operator has a return type and a parameter list. Created a class called complex and within that
declared two variables and defined two methods read and display, read() is used for entering the
values for real part and imaginary part .display() method is used to view the complex number. And
based on user choice can enter two complex numbers to perform required operations.

11. Write a C++ program to write number 1 to 100 in a data file NOTES.TXT
Program:

#include<fstream>

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

#include<iostream>
using namespace std;
int main()
{
ofstream fout;
fout.open("NOTES.TXT");
for(int i=1;i<=100;i++)
fout<<i<<endl;
fout.close();
return 0;
}

Output:

Object Oriented Programming with C++ Lab Manual


ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

Description:

In the above program, for creation of a file or opening of a file, we have to use fopen () method. And
created a file called "Notes.txt". In that file contains data like the numbers from 1 to 100. After
reading or writing of a file we need to close the file with fclose() method.

12. Write a function in C++ to count and display the number of lines not starting with alphabet 'A'
present in a text file "STORY.TXT". Example: If the file "STORY.TXT" contains the following
lines, The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password. The function should display the output as 3
Program:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
ifstream fin;
fin.open("story1.txt");
char str[80];
int count=0;
while(!fin.eof())
{
fin.getline(str,80);
if(str[0]!='A')
Object Oriented Programming with C++ Lab Manual
ANIL NEERUKONDA INSTITUTE OF TECHNOLOGY & SCIENCES CSE DEPARTMENT

count++;
}
cout<<"Number of lines not starting with A are "<<count;
fin.close();
}

Output:

Description: The program is used for string matching. Here the program counts how many lines of
text is not starting with a letter ‘A’. This program can be used to filter unwanted text from a file.

Object Oriented Programming with C++ Lab Manual

You might also like