BAI233001 - Zoha Hameed (Lab 12)

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

Lab Tasks (12)

Zoha Hameed
BAI233001
Course Title: PF Lab
Course Instructor: Sir Taimoor Riaz

BS Artificial Intelligence
Software Engineering
Capital University of Science and Technology
Islamabad Campus
Fall 2023
Using Strings in C++

Output:
Difference between using cin and getline while using strings

cin considers “space” as terminating character.

For Example:

Getline considers “enter” as terminating point.


Task-1
Write a C++ program that asks the user to enter a string. The program should count total number
of vowels
in the input string. The output of the program should report vowel count for each vowel character
(a, e, i, o , u), and total or sum of all vowel count.

Source Code:

#include<iostream>
#include<string>
using namespace std;
int main(){
int counta = 0;
int counte = 0;
int counti = 0;
int counto = 0;
int countu = 0;
string name;
cout << "-----------------------------------" << endl;
cout << "Enter something" << endl;
cout << "-----------------------------------" << endl;
getline(cin, name);
// cout << name.length() << endl;

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


if (name[i] == 'a' || name[i] == 'A' ) {
counta++;
}
if (name[i] == 'e' || name[i] == 'E') {
counte++;
}
if (name[i] == 'i' || name[i] == 'I') {
counti++;
}
if (name[i] == 'o' || name[i] == 'O') {
counto++;
}
if (name[i] == 'u' || name[i] == 'U') {
countu++;
}

}
cout << "-----------------------------------" << endl;
cout << "Total Vowels: " << counta + counte + counti+ counto +countu << endl;
cout << " A: " << counta<<endl;
cout << " E: " << counte << endl;
cout << " I: " << counti << endl;
cout << " O: " << counto << endl;
cout << " U: " << countu << endl;
system("pause");
return 0;
}
Output:

Task-2
Write a C++ program that asks the user to enter a string. After that, the program should ask for
another string in a variable named “str_new”. Take another string input in the variable named
“str_old”. In the end, the program should replace the “str_new” with “str_old”. In the end the
program prints the update string.

Task-3
Write a C++ program that counts total words in a sentence. First, get input in a string variable
“prg” for a paragraph (terminated by enter key). Your program should count total number of
words in the paragraph (using find function of string).

Source Code:
#include<iostream>
#include<string>
using namespace std;
int main(){

string prg;
int wordcount = 0;
int space_pos = -1;
cout << "Enter a sentence terminated by enter key:\n\n";
getline(cin, prg);
do {
space_pos = prg.find(" ", space_pos+1);
if (space_pos > 0)
wordcount++;
}
while (space_pos > 0);

wordcount = wordcount + 1;
cout << "\n Your entered sentence contains: " << wordcount << " words"<<endl;
system("pause");
return 0;
}

Output:

Source Code:
#include<iostream>
#include<string>
using namespace std;
int main(){
string Str;
int wordcount = 0;
int space = -1;
cout << "Enter a string: ";
getline(cin, Str);
cout << "----------------------------------------" << endl;
cout << " (a)- Length of string is: " << Str.length()<<endl;
cout << "----------------------------------------" << endl;
cout << " (b)- In reverse order: " << endl;
for (int i = Str.length(); i >= 0; i--) {
cout << Str[i];
}
cout << endl;
cout << "----------------------------------------" << endl;
Str = Str + " Hello World";
cout << "(c)- Concatenated String: " << Str << endl;
cout << "----------------------------------------" << endl;
do {
space = Str.find(" ", space+=1);
if (space > 0) {
wordcount++;
}
} while (space > 0);
wordcount += 1;
cout << "(d)-Total words are : " << wordcount << endl;
cout << "----------------------------------------" << endl;
for (int i = 0; i<Str.length(); i++) {
if (Str[i] >= 'A' && Str[i] <= 'Z') {
Str[i] = Str[i] + 32;
}
}
cout << " (d)- In lower case : " << Str << endl;
cout << "----------------------------------------" << endl;
system("pause");
return 0;
}

Lab Task : 02
Write a C++ program which repeatedly asks the user to enter their contact numbers. Create a
string variable named “Phone_No” and take the input in the following pattern: “+92423672123”
where the first three characters represent country code (e.g., +92 as shown in the example
pattern), next two characters (e.g., 42 according the shown example number) represent the city
code, while next characters represent the actual number.
Your task is to display appropriate messages for the numbers belonging to three cities: Islamabad
(having city code 51), Lahore (city code 42), and Karachi (city code: 21). Any number having
different country code or city code must be reported as “Un-known number“. Please also ensure
that the user must enter the number according to the shown pattern only and the size of the input
string must be 12 characters.

#include<iostream>
#include<string>
using namespace std;
int main(){
char ch;
string phoneno;
cout << "Enter a phone no: ";
getline(cin, phoneno);

if (phoneno.length() == 12) {
if (phoneno[0] == '+' && phoneno[1] == '9' && phoneno[2] == '2') {
do {
if (phoneno[3] == '4' && phoneno[4] == '2') {
cout << "This number belongs to Lahore" << endl;
}
else if (phoneno[3] == '2' && phoneno[4] == '1') {
cout << "This number belongs to Karachi" << endl;
}
else if (phoneno[3] == '5' && phoneno[4] == '1') {
cout << "This number belongs to Islamabad" << endl;
}
else
{
cout << "Un-known number" << endl;
}
cout << "Do you want to continue?";
cin >> ch;

} while (ch == 'y' || ch == 'Y');


}
else {
cout << "Un-known number";
}
cout << "End of Program!.." << "Bye!" << endl;
}
system("pause");
return 0;
}

Task : 5

You might also like