0% found this document useful (0 votes)
10 views4 pages

Rahmacplab 10

The document contains 4 tasks related to string manipulation in C++. Task 1 counts the number of occurrences of a character in a string. Task 2 checks if a string is a palindrome. Task 3 compares the lengths of two strings. Task 4 assigns one string to another.

Uploaded by

rzamir512
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)
10 views4 pages

Rahmacplab 10

The document contains 4 tasks related to string manipulation in C++. Task 1 counts the number of occurrences of a character in a string. Task 2 checks if a string is a palindrome. Task 3 compares the lengths of two strings. Task 4 assigns one string to another.

Uploaded by

rzamir512
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/ 4

Rahma Zamir BSCS 1-A 02-134222-096

Computer Programming Lab 10: String

Task 01
#include<iostream>
#include<string>
using namespace std;
void main()
{
string str;
char search;
int length, num = 0;
cout << "Enter string: " << endl;
cin >> str;
cout << "Enter character to search" << endl;
cin >> search;
length = str.length();
for (int i = 0; i < length; i++)
{
if (str[i] == search)
{
num += 1;
}
}
if (num > 0)
{
cout << "Character found " << num << " times. \n";
}
else {
cout << "Character not found. \n";
}
system("pause");
}
Rahma Zamir BSCS 1-A 02-134222-096

Task 02
#include <iostream>
#include<string>
using namespace std;
string isPalindrome(string S)
{
string P = S;
reverse(P.begin(), P.end());
if (S == P)
{
cout << "This string is a palindrome" << endl;
}
else
{
cout << "This string is not a palindrome" << endl;
}
return S;
}
void main()
{
string S;
cout << "Enter your word: ";
cin >> S;
cout << isPalindrome(S);
system("pause");
}
Rahma Zamir BSCS 1-A 02-134222-096
Task 03
#include <iostream>
#include<string>
using namespace std;
string compare(string str1, string str2)
{
string ans;
if (str1.length() > str2.length())
{
ans = "first";
}
else if (str2.length() > str1.length())
{
ans = "second";
}
else {
ans = "e";
}
return ans;
}
void main()
{
string str1, str2, a;
cout << "Enter first word: \n";
cin >> str1;
cout << "Enter your second word:\n";
cin >> str2;
a = compare(str1, str2);
if (a == "e")
{
cout << "Both words are equal in length.\n";
}
else {
cout <<"The " << a << " word is longer.\n";
}
system("pause");
}
Rahma Zamir BSCS 1-A 02-134222-096
Task 04
#include <iostream>
#include<string>
using namespace std;
void main()
{
string s1, s2;
cout << "Enter string s1: ";
getline(cin, s1);
s2 = s1;
cout << "string s2 = " << s2 << endl;
system("pause");
}

You might also like