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

NAME: MUDSSAR ASLAM

Reg no: BSE231023


Task 1:

#include <iostream>

#include<string>

using namespace std;

int main()

string str;

cout<<" enter the string :\n";

getline(cin,str);

cout<<" your string is "<<str<<endl;

//for string length

cout<<" your string length is "<<str.length();

int w_count=0;

for(int i=str.length()-1;i>=0;i--)

if(str[i]==' ')

w_count++;

str.append("hellow world");

cout<<endl;

cout<<" updated array is "<<endl<<str<<endl;

cout<<" vowel count is "<<w_count+1<<endl;


// Convert whole string in lowercase.

for (int i = 0; i < str.length(); i++) {

if (str[i] >= 'A' && str[i] <= 'Z') {

str[i] += 32;

cout << "The string in lowercase is "<<endl<<str<<endl;

Task 2
#include <iostream>
#include <string>

using namespace std;

int main() {
string str;

cout << "Enter a string: ";


getline(cin, str);

int a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count = 0;

for (int i = 0; i < str.length(); i++) {


if (str[i] == 'a') {
a_count++;
} else if (str[i] == 'e') {
e_count++;
} else if (str[i] == 'i') {
i_count++;
} else if (str[i] == 'o') {
o_count++;
} else if (str[i] == 'u') {
u_count++;
}
}
cout << "The number of 'a' vowels is " << a_count << endl;
cout << "The number of 'e' vowels is " << e_count << endl;
cout << "The number of 'i' vowels is " << i_count << endl;
cout << "The number of 'o' vowels is " << o_count << endl;
cout << "The number of 'u' vowels is " << u_count << endl;

cout << "The total number of vowels is " << a_count + e_count + i_count + o_count + u_count
<< endl;

return 0;
}

Task 3
#include <iostream>
#include <string>

using namespace std;

int main() {
string Phone_No;

do {
cout << "Enter a number: ";
getline(cin, Phone_No);

if (Phone_No.length() != 12) {
cout << "Invalid input. The input string must be of size 12." << endl;
continue;
}

if (Phone_No[0] != '+' || Phone_No[1] != '9' || Phone_No[2] != '2') {


cout << "Invalid input. The country code must be +92." << endl;
continue;
}

int city_code = Phone_No[3] - '0' * 10 + Phone_No[4] - '0';

switch (city_code) {
case 51:
cout << "This number belongs to Islamabad." << endl;
break;
case 42:
cout << "This number belongs to Lahore." << endl;
break;
case 21:
cout << "This number belongs to Karachi." << endl;
break;
default:
cout << "Un-known number." << endl;
break;
}

cout << "Do you want to enter another number (y/n): ";
char ch;
cin >> ch;
}
while ( ch == 'y' || ch=='Y');

cout << "End of program." << endL;


}

Task 4
#include <iostream>
#include<string>
using namespace std;

int main() {
string str, str_new, str_old;

cout << "Please enter a string: ";


getline(cin, str);

cout << "Enter word to replace: ";


cin >> str_old;

cout << "Enter word to replace with: ";


cin >> str_new;

// Replace the old word with the new word.


str.replace(str.find(str_old), str_old.length(), str_new);

// Print the updated string.


cout << str << endl;

return 0;
}

You might also like