Programming fundamentals
Lab Report #11
By
NAME Regestration Number
MOBEEN AYYAZ FA23-BCE 2B-068
For the course
Programming Fundamentals
Submitted to:
Usman Khalid
Department of Electrical & Computer
Engineering
1
COMSATS University Islamabad - Abbottabad
Campus
Task 1:
Write a program that reads a string and prints the number of vowels
letters.
code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch[100];
int vowel = 0;
cout << "Enter a string: ";
cin.getline(ch, 100);
for (int i = 0; ch[i] != '\0'; ++i)
{
if (ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o' || ch[i]
==
'u' ||
ch[i] == 'A' || ch[i] == 'E' || ch[i] == 'I' || ch[i] == 'O' || ch[i]
== 'U')
{ vowel++; }
}
cout << "Number of vowels: " << vowel << endl;
return 0;
}
Output:
Task 2:
Write a program that concatenates two strings and find the length of
each string.
Code:
2
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2, combine;
int length1, length2;
cout << "Enter the first string: ";
getline(cin, str1);
cout << "Enter the second string: ";
getline(cin, str2);
length1 = str1.length();
length2 = str2.length();
combine = str1 + str2;
cout << "First string: " << str1 <<"\n";
cout << "Length of first string: " << length1 <<"\n";
cout << "Second string: " << str2 <<"\n";
cout << "Length of second string: " << length2 << "\n";
cout << "Combined string: " << combine << "\n";
return 0;
}
Output:
Task 3:
Write a program to check if a string contains a specific substring?
Code:
#include <iostream>
using namespace std;
int main()
{
int i=0, j=0, l1, l2;
char str[]= {"my name is Man Pala The Great" }, name[]=
{"Pala" };
cout << "string: ";
while(str[i]!='\0')
{
3
cout<<str[i];
i++; }
i=0;
cout << "\nsubstring: ";
while(name[i]!='\0')
{
cout<<name[i];
i++; }
l1 = 0, l2 = 0;
while (str[l1] != '\0') l1++;
while (name[l2] != '\0') l2++;
bool found = false;
for ( i = 0; i <= l1 - l2; i++)
{
for (j = 0; j < l2; j++)
{
if (str[i + j] != name[j]) break; }
if (j == l2)
{
found = true;
break; } }
if (found)
{
cout << "\n\nThe string contains the substring."; }
else
{
cout << "\n\nThe string does not contain the
substring.";}
return 0; }
Output:
Task 4:
4
Write a program that compares two strings lexicographically.
Code:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int result;
string str1= "my name is khan baba";
string str2= "my name is khan baba and i am in BCE 2B";
cout<<"string 1: "<<str1;
cout<<"\nstring 2: "<<str2;
result=str1.compare(str2);
if(result==0)
{
cout<<"\n\n String 1 is eqaul to string";
}
else if(result<0)
{
cout<<" \n\nstring is less than string 2";
}
else
{
cout<<"\n\nstring is greater than string 2";
}
}
Output:
5
Task 5:
Write a program that reads a string and print the string with the first
letter capitalized and the remaining in lower case.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int result, length;
string str= "my name is Khan Baba and i am in BCE 2B";
cout<<"\nstring : "<<str;
length=str.length();
for(int i=0; i<length; i++)
{
if(i==0)
{
str[i]= toupper(str[i]); }
else
{
str[i]= tolower(str[i]); } }
cout<<"\nstring : "<<str;
return 0;
}
Output:
6
Task 6:
Write a program that convert a string to uppercase.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int result, length;
string str= "my name is Khan baba and i am in BCE 2B";
cout<<"\nstring : "<<str;
length=str.length();
for(int i=0; i<length; i++)
{
str[i]= toupper(str[i]); }
cout<<"\nstring : "<<str;
return 0;
}
Output:
7
8