0% found this document useful (0 votes)
15 views

P-T LAB 4 Assign

This document contains the code and output for 4 tasks completed as part of a lab assignment for a C++ Programming Lab course. The tasks involve basic string manipulation programs - reading/displaying a string, counting the length of a string, accessing elements of a 2D array of strings, printing a string with indexes, counting words in a string, and concatenating two strings. The document provides the name of the lab instructor, course details, student name and roll number, and the code and output for each task.

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)
15 views

P-T LAB 4 Assign

This document contains the code and output for 4 tasks completed as part of a lab assignment for a C++ Programming Lab course. The tasks involve basic string manipulation programs - reading/displaying a string, counting the length of a string, accessing elements of a 2D array of strings, printing a string with indexes, counting words in a string, and concatenating two strings. The document provides the name of the lab instructor, course details, student name and roll number, and the code and output for each task.

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/ 10

Lab Instructor: Mr.

Nouman Asim

DEPARTMENT OF COMPUTER SCIENCE

SIR SYED CASE INSTITUTE OF TECHNOLOGY

Lab No. 4

Course Name : PT LAB


Submitted by: huzafia rasheed
Roll No.:2330-0074
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