Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PT LAB 5 TASK

NAME : Karnal Ahmed Yar Khan

DEPARTMENT : BSCS

ROLL NO# : 2330-0092

Date: 14-3-2024
Task 1: Write a program in C++ to print the first character of
your name 6 times by using the string. Taken the input from
user through Getline.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name[]=;
cout << "Please enter your name: ";
getline(cin, name);

for (int i = 0; i < 6; ++i)


{
std::cout << name[0];
}

cout <<endl;

return 0;
}
Output:

Task 2
Code:
#include<iostream>
#include <string>
using namespace std;

int main()

{
char str[]="hello";
char str2[]="world";
cout<<"string 1 is:";
cout<<str;
cout<<"\n\nstring 2 is:";
cout<<str2;

strcpy(str,str2);
cout<<"\n\nstring copy is:";
cout<<str;
strcat(str,str2);
cout<<"\n\nstring cat is:";
cout<<str;
return 0;
}
Output:

Task 3:
Code:
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
string str1, str2;
cout << "Enter the first string: ";
getline(cin, str1);
cout << "Enter the second string: ";
getline(cin, str2);

string concatStr = str1 + " " + str2;


cout << "Concatenated string: " << concatStr << endl;

string userInput;
cout << "Enter a string of 10 characters: ";
cin >> userInput;
transform(userInput.begin(), userInput.end(),
userInput.begin(), ::tolower);
string output = userInput.substr(1, 9);
cout << "Output: " << output << endl;

string inputString;
cout << "Enter a string: ";
cin.ignore();
getline(cin, inputString);

if (inputString.length() >= 2) {
cout << "Two characters: " << inputString[0] <<
inputString[1] << endl;
} else {
cout << "String should have at least two characters." <<
endl;
}

string str = "HeloPTClass";


string updateStr, newStr;
cout << "Enter the string to update or add at index 2: ";
cin.ignore();
getline(cin, updateStr);

if (str.length() >= 2) {
newStr = str.substr(0, 2) + updateStr + str.substr(2,
str.length() - 2);
cout << "Updated string: " << newStr << endl;
} else {
cout << "String should have at least two characters." <<
endl;
}

return 0;
}
Output:
Task 4 :
Code:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char str1[100];
char str2[100];

cout << "Enter the first string: ";


gets(str1);
cout << "Enter the second string: ";
gets(str2);

int result = strcmp(str1, str2);

if (result < 0)
cout << "The first string is lexicographically smaller than
the second string." <<endl;
else if (result > 0)
cout << "The first string is lexicographically greater than
the second string." <<endl;
else
cout << "The first string is equal to the second string."
<<endl;

return 0;
}
Output:
TASK 5:
Code:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
string input;
cout << "Enter a string with random characters: ";
getline(cin, input);

sort(input.begin(), input.end());
cout << "The sorted string alphabetically: " <<input <<endl;

char source[]="this is a string to be copied";


char destination[]="this is a string to be copied";

cout << "Enter the source string: ";


gets(source);

strcpy(destination, source);

cout << "The copied string: ";


puts(destination);

return 0;
}
Output:

You might also like