Comsats: Block F, NISTE Building, H-8/1 Islamabad Pakistan
Comsats: Block F, NISTE Building, H-8/1 Islamabad Pakistan
Assignment 02
Total Marks = 10
Q#1) Write a program that substitutes an overloaded += operator for the overloaded
+ operator in the STRPLUS program.
Answer:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
main ( )
{
string s1 ( "good" );
string s2 ( "luck" );
{
cout << "The orignal string is: " << s1 << "." << endl;
cout << "The orignal string is " << s2 << "." << endl;
char *s3 = "Star";
cout << "The c-style string is " << s3 << "." << endl;
char c1 = '!';
cout << "The character constant is " << c1 << "." << endl;
string s12 = s1 + s2;
cout << "The string s1 & s2 is: " << s12 << endl;
string s1s3 = s1 + s3;
cout << "The string s1 & s3 is: " << s1s3 << endl;
string s1s3c1 = s1s3 + c1;
cout << "The string s1 & s3 is: " << s1s3c1 << endl;
}
getch();
}
*****************************************
Q#2) Write a CLR console program that defines a string (as type String^) and then
analyzes the characters in the string to discover the number of uppercase
letters, the number of lowercase letters, the number of non - alphabetic
characters,
and the total number of characters in the string.
Answer:
#include "stdafx.h"
#include<conio.h> // used for getch function
using namespace System;
int main(array<System::String ^> ^args)
{
int caps = 0; //variables are declared here
int small = 0;
int nalpha = 0;
int totalchar = 0;
String^ string = L"Being a Muslim, My responsibility is to follow the teachings of Prophet Muhammad
(PBUH).";
for each(wchar_t ch in string) //conditions are imposed here
{
++totalchar;
if(Char::IsLetter(ch))
{
if(Char::IsUpper(ch))
++caps;
else
++small;
}
if(Char::IsDigit(ch))
++nalpha;
if(Char::IsPunctuation(ch))
++nalpha;
}
Console::WriteLine(string);
Console::WriteLine(L"\nThe total of capitals is {0} and small letters is {1}.", caps, small);
Console::WriteLine(L"The total non-alphabetic is {0} and total characters is {1}.",nalpha, totalchar);
//return 0;
return getch();
}
************************************