Oop Lab 10
Oop Lab 10
Oop Lab 10
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;
if (isupper(text[i]))
{ uppercase++;}
if (isspace(text[i]))
{space++;}
{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;
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;
string a ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
getline(cin,p01);
getline(cin,p02);
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)
else if (keyFound1)
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: