You are on page 1of 7

LAB 12 WORK

EXPERIMENT NO : 12
ROLL NO: 19L-1367

M.MOAZ AWAN

Ex 1 : Write a C++ program that asks the user to enter a phrase


and then a single letter. The program should then report how
many times that letter occurred in the phrase, displaying the letter
with double appropriately.

Code:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char c[1000], letter;
cout<<"Phrase : ";
cin.getline(c,1000,'\n');
cout<<"Letter : ";
cin>>letter;
int count = 0;

for(int i = 0; c[i] != '\0'; i++)


{
if(letter == c[i])
count++;
}
cout << "The Letter "<<"'"<<letter<<"'"<< " occurred " <<
count <<" times in the phrase.";
return 0;
}
Output:

Ex 2:

Write a program that will find the number of vowels in the string.
Define a function that must take the whole string from the main and
print the number of vowels appears in the string?
Code:

#include <iostream>
#include <cstring>
using namespace std;

int phrase(char line[1000])


{
int vowel =0;
for(int i = 0; i<strlen(line); i++)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')

vowel++;
}
return vowel;

int main()
{

char c[1000];
int vowel;
cout<<"Enter the phrase : ";
cin.getline(c,1000,'\n');
vowel = phrase(c);
cout<<"Vowels : "<<vowel<<endl;
return 0;
}

Output:

Exercise –3 (10 points)


You are required to write a program which will take
input from user in two integer arrays. The program should compare both
arrays for checking if both arrays are totally identical (Exactly same). Write a
boolean function which return true if arrays are totally identical otherwise
return false. Example of two identical (Exactly same) arrays:

Code:
#include<iostream>
#include<cstring>
using namespace std;
bool IsIdenticalArrays(int a[],int b[],int s)
{
int c;
for( c=0;c<s;c++)
{
if(a[c]==b[c])
continue;
return false;
}

return true;

}
int main()
{
int Array1[6],Array2[6];
int elements=6;
cout<<"Enter Array1 : " <<endl;

for (int i=0;i<6;i++)

cin>>Array1[i];

cout<<"Enter Array2 : " <<endl;

for (int j=0;j<6;j++)

cin>>Array2[j];

if(IsIdenticalArrays(Array1,Array2,elements))

cout<<"Exactly Same.";
else
cout<<"Not Same.";
return 0;
}

Output:

Exercise – 4 (10 points) :

Write a function PickLarger( ) which compares the corresponding


elements of array, picks the larger element among them and saves the
larger element in the output array.
Code:
#include<iostream>
using namespace std;
void PickLarger(int x[],int y[],int z[],int l)
{
for(int m=0;m<l;m++)
if(x[m]>y[m])
z[m]=x[m];
else
z[m]=y[m];
}
int main()
{

int a[6],b[6],c[6],d;

d=6;
cout<<"Enter Array1 : ";

for(int i=0;i<6;i++)
cin>>a[i];
cout<<"Enter Array2 : ";
for(int j=0;j<6;j++)
cin>>b[j];

PickLarger(a,b,c,d);
cout<<"Output Array : ";
for(int k=0;k<6;k++)
cout<<c[k]<<" ";
cout<<endl;

return 0;
}

Output:

You might also like