0% found this document useful (0 votes)
17 views11 pages

PT Lab 4

This document contains the code and output for 4 tasks completed as part of a lab assignment for a Computer Science course at Sir Syed Case Institute of Technology. The tasks involve basic string manipulation in C++, including reading and displaying a string, calculating string length, accessing characters at specific indices, concatenating strings, and counting words in a string. The document was submitted by student Qasim Tariq for the Programming Techniques lab taught by instructor Mr. Nouman Asim.

Uploaded by

2004qasimtariq
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)
17 views11 pages

PT Lab 4

This document contains the code and output for 4 tasks completed as part of a lab assignment for a Computer Science course at Sir Syed Case Institute of Technology. The tasks involve basic string manipulation in C++, including reading and displaying a string, calculating string length, accessing characters at specific indices, concatenating strings, and counting words in a string. The document was submitted by student Qasim Tariq for the Programming Techniques lab taught by instructor Mr. Nouman Asim.

Uploaded by

2004qasimtariq
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/ 11

Lab Instructor: Mr.

Nouman Asim

DEPARTMENT OF COMPUTER SCIENCE

SIR SYED CASE INSTITUTE OF TECHNOLOGY

Lab No. 4

Course Name : PT LAB


Submitted by: Qasim
Tariq
Roll No.:2330-0161
Class Section: /FA23:BS(CS)/
TASK : 1a
CODE :
#include<iostream>
using namespace std;
int main()
{
char str[80];
cout<< "Enter a string: ";
cin>>str;
cout<< "Here is your string: ";
cout<<str<<"\n";
return 0;
}
OUTPUT :
TASK :1b
CODE :
#include<iostream>
using namespace std;
int main()
{
int length=0;
char *ptr;
char original[] = {'C','+','+',' ','i','s',' ','b','e','s','t','\0'};
ptr=original;
for(int index=0;ptr[index]!='\0';index++)
length++;
cout<<length;
return 0;
}
OUTPUT :

TASK :1c
CODE :
#include<iostream>
using namespace std;
int main()
{
char city[80] =
"Islamabad";
char places[4][80]=
{"Lahore", "Hanoi",
"Karachi", "Madrid"};
cout<<places[0] <<'\n';
cout<<places[2][5]<<'\n';
cout<<places[1][4]<<'\n';
cout<< city <<'\n';
cout<<city[4]<<'\n';
return 0;
}
OUTPUT :
TASK : 2
CODE :
#include<iostream>
using namespace std;
void printNameWithIndex(const char* name)
{
cout<<"Name: "<<name<<endl;
cout << "Index:"<<endl;
for(int i=0;name[i]!='\0';++i)
{
cout<<name[i]<<": "<< i<<endl;
}
}
int main() {
const char name[] = "HUZAIFIA";
printNameWithIndex(name);
return 0;
}

OUTPUT :

TASK : 3
CODE :
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int countWords(const string& str)
{
istringstream i(str);
int count=0;
string word;
while(i>>word)
{
count++;
}
return count;
}
int main() {
string inputString = "this is a string for words";
int wordCount=countWords(inputString);
cout<<"Number of words in the string:
"<<wordCount<<endl;

return 0;
}
OUTPUT :

TASK : 4
CODE :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string string1, string2;
cout << "enter the first string: ";
getline(cin, string1);
cout << "enter the second string: ";
getline(cin, string2);
string concatenated_string = string1 + string2;
cout << "After concatenation the string is:\n\"" <<
concatenated_string << "\"" << endl;

return 0;
}

You might also like