0% found this document useful (0 votes)
48 views

Comsats: Block F, NISTE Building, H-8/1 Islamabad Pakistan

This document contains an assignment for a visual programming course, with two questions asking to write code. The first question asks to overload the += operator to concatenate two strings, and the second asks to write a C# program that analyzes the characters in a string to count uppercase, lowercase, non-alphabetic, and total characters.

Uploaded by

Waqar Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Comsats: Block F, NISTE Building, H-8/1 Islamabad Pakistan

This document contains an assignment for a visual programming course, with two questions asking to write code. The first question asks to overload the += operator to concatenate two strings, and the second asks to write a C# program that analyzes the characters in a string to count uppercase, lowercase, non-alphabetic, and total characters.

Uploaded by

Waqar Hassan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMSATS Institute of Information Technology (Virtual Campus)

Block F, NISTE Building, H-8/1 Islamabad Pakistan

Subject: Visual Programming


( CSC444)

Assignment 02

Total Marks = 10

NAME : QULIB ABBAS REG:SP16Mcs064

Q#1) Write a program that substitutes an overloaded += operator for the overloaded
+ operator in the STRPLUS program.

This operator should allow statements like s1 += s2;

where s2 is added (concatenated) to s1 and the result is left in s1.


The operator should also permit the results of the operation to be used in other
calculations, as in s3 = s1 += s2;

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();
}

************************************

You might also like