You are on page 1of 4

HASSAN RASIF CP LAB 13

02-235222-014

EXERCISE 01)
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
int count = 0;
string text;
string letter;
cout << "Enter any text: ";
getline(cin, text);
int size = text.size();
cout << "Enter letter you want to search in the given text: ";
cin >> letter;
int index = text.find(letter);
cout << "Searched letter is found at ";
for (int i = 0; i < size; i++) {
if (text[index] == text[i]) {
count++;
cout << i << " ";
}
}
cout << "index" << endl;
cout << "Occured " << count << " times." << endl;
}
HASSAN RASIF CP LAB 13
02-235222-014

EXERCISE 02)

#include <iostream>

#include <string>

#include <cstring>

using namespace std;

int main () {

string str1="Kaif";

string str2="Zonish";

int str_one_size=str1.size();

int str_two_size=str2.size();

if(str_one_size < str_two_size) {

cout << "String 1 is smaller than String 2" << endl;

else if(str_two_size < str_one_size) {

cout << "String 2 is smaller than String 1" << endl;

else if(str_one_size == str_two_size) {

cout << "Both strings are equal" << endl;

else {

cout << "\0";

}
HASSAN RASIF CP LAB 13
02-235222-014

EXERCISE 03)
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
string str1 = " MY NAME IS HASSAN RASIF";
string str2;
str2 = str1;
cout << str2 << endl;
}
HASSAN RASIF CP LAB 13
02-235222-014

EXERCISE 04
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
string str1 = "MY NAME IS HASSAN ";
cout << "First character is " << str1.front() << endl;
cout << "Last character is " << str1.back() << endl;
}

You might also like