0% found this document useful (0 votes)
6 views5 pages

CPP_Programs_FileHandling_Pointers_Strings

The document is an assignment by Muhammad Rayan for a programming fundamentals course, focusing on C++ programming concepts such as file handling, pointers, and strings. It includes code examples for writing and reading files, counting words and sentences, using pointers with integers and arrays, and performing string operations like concatenation and comparison. The assignment is submitted to Sir Tajjamul at the National University of Modern Languages, Lahore.

Uploaded by

rayannuml313
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)
6 views5 pages

CPP_Programs_FileHandling_Pointers_Strings

The document is an assignment by Muhammad Rayan for a programming fundamentals course, focusing on C++ programming concepts such as file handling, pointers, and strings. It includes code examples for writing and reading files, counting words and sentences, using pointers with integers and arrays, and performing string operations like concatenation and comparison. The assignment is submitted to Sir Tajjamul at the National University of Modern Languages, Lahore.

Uploaded by

rayannuml313
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/ 5

ASSIGNMENT NO : 03

BY

MUHAMMAD RAYAN

BSCS-M1(1ST SEMESTER)

SUBMITTED TO : SIR TAJJAMUL

SUBJECT : PROGRAMING FUNDAMENTALS

DEPARTMENT OF COMPUTER SCIENCE


NATIONAL UNIVERSITY OF MODERN LANGUAGES

LAHORE

C++ Programs: File Handling, Pointers,


and Strings
📂 File Handling

1. Write to a file

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream fout("example.txt");
fout << "Hello World!";
fout.close();
return 0;
}

2. Read from a file

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream fin("example.txt");
string data;
while (getline(fin, data)) {
cout << data << endl;
}
fin.close();
return 0;
}

3. Word and Sentence Count

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream fin("example.txt");
string word;
int wordCount = 0, sentenceCount = 0;
char ch;
while (fin.get(ch)) {
if (ch == ' ' || ch == '\n') wordCount++;
if (ch == '.' || ch == '?' || ch == '!') sentenceCount++;
}
fin.close();
cout << "Words: " << wordCount + 1 << endl;
cout << "Sentences: " << sentenceCount << endl;
return 0;
}

📍 Pointers

1. Pointer to int

#include <iostream>
using namespace std;

int main() {
int a = 10;
int *p = &a;
cout << *p << endl;
return 0;
}

2. Pointer and array

#include <iostream>
using namespace std;

int main() {
int arr[3] = {1, 2, 3};
int *p = arr;
for (int i = 0; i < 3; i++) {
cout << *(p + i) << " ";
}
return 0;
}

3. Swap using pointers

#include <iostream>
using namespace std;

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

int main() {
int a = 5, b = 10;
swap(&a, &b);
cout << a << " " << b << endl;
return 0;
}

🧵 Strings

1. Concatenation

#include <iostream>
#include <string>
using namespace std;

int main() {
string s1 = "Hello", s2 = "World";
string s3 = s1 + " " + s2;
cout << s3 << endl;
return 0;
}

2. Length and Substring

#include <iostream>
#include <string>
using namespace std;

int main() {
string s = "Programming";
cout << s.length() << endl;
cout << s.substr(0, 6) << endl;
return 0;
}

3. Compare Strings

#include <iostream>
#include <string>
using namespace std;

int main() {
string a = "Apple", b = "Banana";
if (a == b)
cout << "Equal" << endl;
else
cout << "Not Equal" << endl;
return 0;
}

You might also like