0% found this document useful (0 votes)
49 views4 pages

Name: Abdul Haseeb Memon Roll No: 2019-CS-056 Sec: 5-B: Lab 1 Task 1

The document contains the details of a student named Abdul Haseeb Memon with roll number 2019-CS-056 studying in section 5-B. It describes 3 programming tasks they completed for their Computer Science lab assignments. Task 1 involves a program to count the frequency of alphabet letters in a string. Task 2 demonstrates reading from and writing to a text file. Task 3 shows searching for and replacing a word within a text file.

Uploaded by

UZAIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Name: Abdul Haseeb Memon Roll No: 2019-CS-056 Sec: 5-B: Lab 1 Task 1

The document contains the details of a student named Abdul Haseeb Memon with roll number 2019-CS-056 studying in section 5-B. It describes 3 programming tasks they completed for their Computer Science lab assignments. Task 1 involves a program to count the frequency of alphabet letters in a string. Task 2 demonstrates reading from and writing to a text file. Task 3 shows searching for and replacing a word within a text file.

Uploaded by

UZAIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 ";
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy