You are on page 1of 12

PPL Assignment 7

- U18CO072
- Hardik Upadhyay

1. Write a program that reads a text file and creates another file that is identical except that
every sequence of consecutive blank space is replaced by a single space.

Code:
#include<bits/stdc++.h>
using namespace std;

const int N = 100100;

int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
string str;
while(getline(cin, str)) {
for(int i = str.size() - 1; i >= 0; i--) {
if(str[i] == ' ' && str[i] == str[i-1]) {
str.erase(str.begin() + i);
}
}
cout << str << endl;
}
return 0;
}

Input.txt

Output.txt
2. Write a program to copy the contents of a source file student1.txt to a destination file
student2.txt character by character.

Code:
#include<bits/stdc++.h>
using namespace std;

const int N = 100100;

int main()
{
freopen("student1.txt","r",stdin);
freopen("student2.txt","w",stdout);
char ch;
while(cin >> noskipws >> ch){
cout << ch;
}
return 0;
}

Student1.txt

Student2.txt

3. Write a program that uses command-line argument to copy the contents of a file A.txt
into another file B.txt by reversing the case of the characters. E.g. File A.txt: aBCd File
B.txt: AbcD.
Code:
#include<bits/stdc++.h>
using namespace std;

const int N = 100100;

int main(int argc, char **argv)


{
if(argc != 3) {
cerr << "Invalid syntax. Expected syntax <OUTPUT.exe> fileToRead fileToWrite";
exit(1);
}
freopen(argv[1],"r",stdin);
freopen(argv[2],"w",stdout);
char ch;
while(cin >> noskipws >> ch){
if(ch >= 'a' && ch <= 'z') ch = ch - 'a' + 'A';
else if(ch >= 'A' && ch <= 'Z') ch = ch - 'A' + 'a';
cout << ch;
}
return 0;
}

A.txt

B.txt

4. Write a program for swapping two values of different data types using template

Code:

#include<bits/stdc++.h>
using namespace std;

template<typename T>

T my_swap(T &a, T &b) {

T t = a;

a = b;

b = t;

int main(int argc, char **argv)

string a = "A", b = "B";

cout << a << " " << b << endl;

my_swap(a, b);

cout << a << " " << b << endl;

int n1 = 1, n2 = 2;

cout << n1 << " " << n2 << endl;

my_swap(n1, n2);

cout << n1 << " " << n2 << endl;

Output:
5.

Code:

#include<bits/stdc++.h>

using namespace std;

template<class T>

class My_Vector{

T *a;

int size;

public:

explicit My_Vector(int size) : size(size) {

a = new T[size];

void create() {

for(int i = 0; i < size; i++){

cout << "a[" << i << "] : " << endl;

cin >> a[i];

}
}

void modify() {

int index;

T val;

cout << "Enter index to modify: " << endl;

cin >> index;

if(index >= size) {

cerr << "Index out of bounds" << endl;

exit(1);

cout << "Enter the modified value: " << endl;

cin >> val;

a[index] = val;

void print() {

cout << "Contents of vector: " << endl;

for(int i = 0; i < size; i++) {

cout << a[i] << endl;

};

int main()
{

int sz;

cout << "Enter size of the integer vector" << endl;

cin >> sz;

cout << "Created vector of integers of size " << sz << endl;

My_Vector<int> a(sz);

a.create();

a.modify();

a.print();

int sz;

cout << "Enter size of the string vector" << endl;

cin >> sz;

cout << "Created vector of strings of size " << sz << endl;

My_Vector<string> a(sz);

a.create();

a.modify();

a.print();

Output
6. Create a generic class stack using template and implement common Push and Pop
operations for different data types.

Code:

#include <iostream>

#include <string>

using namespace std;


#define SIZE 5

template <class T> class Stack {

public:

Stack();

void push(T k);

T pop();

T topElement();

bool isFull();

bool isEmpty();

private:

int top;

T st[SIZE];

};

template <class T> Stack<T>::Stack() { top = -1; }

template <class T> void Stack<T>::push(T k)

if (isFull()) {

cerr << "Stack is full\n";

exit(1);

cout << "Inserted element " << k << endl;

top = top + 1;
st[top] = k;

template <class T> bool Stack<T>::isEmpty()

if (top == -1)

return true;

else

return false;

template <class T> bool Stack<T>::isFull()

if (top == (SIZE - 1))

return 1;

else

return 0;

template <class T> T Stack<T>::pop()

if(isEmpty()) {

cerr << "Stack is empty" << endl;

exit(1);

T popped_element = st[top];
top--;

return popped_element;

template <class T> T Stack<T>::topElement()

if(isEmpty()) {

cerr << "Stack is empty" << endl;

exit(1);

T top_element = st[top];

return top_element;

int main()

Stack<int> integer_stack;

Stack<string> string_stack;

integer_stack.push(2);

integer_stack.push(54);

integer_stack.push(255);

string_stack.push("Stacks");

string_stack.push("are");

string_stack.push("awesome");

cout << integer_stack.pop() << " is removed from stack" << endl;

cout << string_stack.pop() << " is removed from stack " << endl;
cout << "Top element is " << integer_stack.topElement() << endl;

cout << "Top element is " << string_stack.topElement() << endl;

return 0;

You might also like