You are on page 1of 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