Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

OBJECT ORIENTED

PROGRAMMING
USING C++
LAB JOURNAL
EXPERIMENTS Nos. 9 & 11
B.TECH 3RD SEMESTER
2019

Submitted to- Submitted by-


Heisnam Rohen Singh Sir Atiqur Rahman
Assistant Professor Roll no-180710007008
Department Of Branch-Computer Sc.
Computer Sc & Engineering & Engineerig

JORHAT ENGINEERING COLLEGE


JORHAT-785007, ASSAM
Experiment 9: Write a program in C++ that reads a text from keyboard and displays the
following information on screen in two columns:
a) Number of lines
b) Number of words
c) Number of characters

Strings should be left justified and numbers should be right justified in a suitable field width.

Program:
INPUT:

#include<iostream>
#include<conio.h>
#define MAX 1000
using namespace std;

int main()
{
int i=0,line=0, word=0, character=0;
char x[MAX];

while(i<1000)
{
int line=0, word=0, character=0;
cout<<endl<<"Enter any text, terminate by * :\n";
cin.getline(x,MAX,'*');

for(i=0; x[i]!='\0'; i++)


{
character++;

if(x[i]==' ')
word++;

if(x[i]=='\n')
{
line++;
word++;
}
}

if(character>0)
{
word++;
line++;
}

cout<<endl<<"Character:"<<"\t"<<character<<"\n"<<"Word:
"<<"\t"<<word<<"\n"<<"Line: "<<"\t"<<line<<"\n"<<endl;

cout<<"Press any character to exit"<<endl;


getch();
return 0;
}
}
OUTPUT:
Experiment 10: Write a C++ program to implement a dynamic array class and illustrate its
use inside main function.

Program:
INPUT:
#include<iostream>
using namespace std;
class dynamic_array
{
int cap;
int n;
int *arr;
void initialize(int p)
{
for(int i=p;i<cap;i++)
arr[i]=0;
}
void expand()
{
cap*=2;
int *temp= new int[cap];
for(int i=0;i<n;i++)
temp[i]=arr[i];
delete[] arr;
arr=temp;
initialize(n);
}
public:
dynamic_array()
{
cap=3;
n=0;
arr=new int[cap];
initialize(0);
}
~dynamic_array()
{
delete[] arr;
}
int add(int x,int index=0)
{
index=n;
if(n>cap)
this->expand();
else if(index>cap)
throw("Index error");
else
{
n++;
arr[index]=x;
}
}
void display()
{
cout<<"\nTHE ELEMETS ARE :";
for(int i=0;i<n;i++)
cout<<" "<<arr[i];
}
};
int main()
{
dynamic_array a;
a.add(34);a.add(44);a.add(56);a.add(67);
a.display();
return 0;
}
OUTPUT:

You might also like