Professional Documents
Culture Documents
- 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;
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;
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;
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 t = a;
a = b;
b = t;
my_swap(a, b);
int n1 = 1, n2 = 2;
my_swap(n1, n2);
Output:
5.
Code:
#include<bits/stdc++.h>
template<class T>
class My_Vector{
T *a;
int size;
public:
a = new T[size];
void create() {
}
}
void modify() {
int index;
T val;
exit(1);
a[index] = val;
void print() {
};
int main()
{
int sz;
cout << "Created vector of integers of size " << sz << endl;
My_Vector<int> a(sz);
a.create();
a.modify();
a.print();
int 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>
public:
Stack();
T pop();
T topElement();
bool isFull();
bool isEmpty();
private:
int top;
T st[SIZE];
};
if (isFull()) {
exit(1);
top = top + 1;
st[top] = k;
if (top == -1)
return true;
else
return false;
return 1;
else
return 0;
if(isEmpty()) {
exit(1);
T popped_element = st[top];
top--;
return popped_element;
if(isEmpty()) {
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;
return 0;