Professional Documents
Culture Documents
PT Lab 5 Tasuk
PT Lab 5 Tasuk
DEPARTMENT : BSCS
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);
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>
int main()
{
string str1, str2;
cout << "Enter the first string: ";
getline(cin, str1);
cout << "Enter the second string: ";
getline(cin, str2);
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;
}
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];
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;
strcpy(destination, source);
return 0;
}
Output: