[go: up one dir, main page]

0% found this document useful (0 votes)
38 views6 pages

Oop Lab 10

The document discusses 4 programming tasks related to object oriented programming. Task 1 involves counting vowels, spaces, and uppercase letters in a string. Task 2 separates fields in an identification number string. Task 3 generates a random key letter and has two players enter strings to find the key letter first. Task 4 prompts the user for text and returns stats about the text.

Uploaded by

urwazahra01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Oop Lab 10

The document discusses 4 programming tasks related to object oriented programming. Task 1 involves counting vowels, spaces, and uppercase letters in a string. Task 2 separates fields in an identification number string. Task 3 generates a random key letter and has two players enter strings to find the key letter first. Task 4 prompts the user for text and returns stats about the text.

Uploaded by

urwazahra01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Course Title: Object Oriented Programming

Lab 10:
TASK 01:
Task 2:
Write a program which prompts user to enter a string of text. Once entered, you need to
present a summary of the text in the following manner.
Total number of vowels in the text.
Total number of spaces in the text.
Total number of upper case characters in the text.

Code:
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{ string text;
int vowel=0;
int space=0;
int uppercase=0;
cout<<"enter text";
getline(cin,text);

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


{

if (isupper(text[i]))
{ uppercase++;}

if (isspace(text[i]))
{space++;}

if ( text[i]=='a' || text.at(i)=='e'|| text.at(i)=='i' || text.at(i)=='o'||


text.at(i)=='u')

{vowel++;}

cout<< "Vowel:"<<vowel;
cout<<endl;
cout<<"Uppercase:"<<uppercase;
cout<<endl;
cout<<"Space"<<space;
cout<<endl;
system ("pause");
return 0;
}

Output:

Task 3:
Write a program that reads an identification number which could be an ID card number in
the following format: XXXXX-XXXXXXX-X
Using members of the string class separate the different fields of the identification number
and display each field.

Code:
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{ string no;

cout<<"Enter ID card number in the format: XXXXX-XXXXXXX-X : ";


getline(cin,no);
string feild_1=no.substr(0,5);
string feild_2=no.substr(6,7);
string feild_3=no.substr(14,1);

cout<<" feild1: "<<feild_1;


cout<<endl;
cout<<" feild2: "<<feild_2;
cout<<endl;
cout<<" feild3: "<<feild_3;

system ("pause");
return 0;
}

Output:

Task 4:
You need to create a game using strings. First generate a random letter from A to Z, this is
your key and is hidden from the players. Ask player1 and player 2 to enter two strings of
length 10.
The player whose strings contains the key alphabet will win. If both the players have key
alphabets, then the player for which it occurs earlier in the string will win. Example Run:
Key: S (Not visible to user)
Player1: ABDXSCJMNK; Player2: CSTUZWKMIJ
Player 2 wins.
HINT:
string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Generate a random number ‘k’ in range 0 to 25 inclusive. Use s[k] as your random letter

Code:

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
string p01,p02;
int x=rand();

int i = rand() % 26;

string a ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char key= a [i];

cout << "Key: " << key << endl;

cout << "Player 1: ";

getline(cin,p01);

cout << "Player 2: ";

getline(cin,p02);

bool keyFound1= false, keyFound2 = false;


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

if (p01[i]== key)

keyFound1 =true;
break;
}
}
for (int i = 0; i < p02.length(); ++i)
{

if (p02[i] ==key)

{ keyFound2= true;
break;

}
}
if (keyFound1 && keyFound2)

int index1 = p01.find(key);

int index2 =p02.find(key);

if (index1 < index2)


{
cout << "Winner : Player 1" << endl;
}
else
{
cout << "Winner : Player 2" << endl;}
}

else if (keyFound1)

{ cout << "Winner : Player 1" << endl;}

else if (keyFound2)

{
cout << "Winner : Player 2" << endl;
}
else
{
cout << "No one wins. Key not found in any player's string." << endl;}

system ("pause");
return 0;
}

Output:

You might also like