Name : ABDUL HASEEB MEMON
Roll no : 2019-CS-056
Sec : 5-B
Lab 1
Task 1
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string>
using namespace std;
int main() {
char str[10];
for (int i = 0; i < 10; i++)
{
cout<<"enter the alphabet:"<<endl;
cin>>str[i];
cout<<"the alphabet "<<str[i]<<" is pressed."<<endl;
}
int i = 0, alphabet[26] = {0}, j;
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
j = str[i] - 'a';
++alphabet[j];
}
++i;
}
cout<<"Frequency of all alphabets in the string is:"<<endl;
for (i = 0; i < 26; i++)
cout<< char(i + 'a')<<" : "<< alphabet[i]<< endl;
getch();
return 0;
}
LAB 2
Task 1
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char text[200];
fstream file;
file.open ("file.txt", ios::out | ios::in );
cout << "Write text to be written on file." << endl;
cin.getline(text, sizeof(text));
// Writing on file
file << text << endl;
// Reding from file
file >> text;
cout << text << endl;
//closing the file
file.close();
return 0;
}
Task 2
#include <iostream>
#include <fstream>
using namespace std;
int main(){
string search;
ifstream inFile;
string line;
inFile.open("C:/Users/ al azhar shop /Desktop/text.txt");
if(!inFile){
cout << "Unable to open file" << endl;
exit(1);
}
cout << "Enter word to search for: ";
cin >>search;
size_t pos;
while(inFile.good())
{
getline(inFile,line); // get line from file
pos=line.find(search); // search
if(pos!=string::npos) // string::npos is returned if string is not found
{
cout <<search<<" is Found!";
break;
}
}
}
Task 3
#include <iostream>
#include <fstream>
using namespace std;
int main(){
string search;
ifstream inFile;
string line;
string replace;
inFile.open("C:/Users/al azhar shop/Desktop/text.txt");
if(!inFile){
cout << "Unable to open file" << endl;
exit(1);
}
cout << "Enter word to search for: ";
cin >>search;
cout << "Enter word to replace: ";
cin >> replace;
size_t pos;
while(inFile.good())
{
getline(inFile,line); // get line from file
pos=line.find(search); // search
if (pos != std::string::npos){
line.replace(pos, search.length(), replace);
cout << "replaced successfully ";
}
}
}