Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment No 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Fundamentals of Programming

Lab Report 10

Introduction to Address Operators and c-strings


1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: B Group:_
SUBMITTED BY
Name CMS Objective Lab Lab
Theory
(1) Work(3) Task Total
(3)
(3)
Malik Haseeb Ullah 428302

School of Mechanical and Manufacturing


Engineering
Objectives:
 Introduction to Address operators
 Introduction to c-strings

Theory:

1. Address operators:

In C++, a method known as the address-of operator provides the memory


address of a variable. Because they "point" to the variable in memory, the
addresses given by the address-of operator are referred to as pointers. An
ampersand is used to denote the unary address-of operator (&).

If we use Address operators without array then it will give the value different
Like in our first program the value of ‘b’ should be printed first but in actual it
is printed after the value of ‘a’ it means without array the variable that is
declared first will hive output first instead of its value
As we use array in our 2nd program then we get the desired value in a
sequence.

2. C-strings:

C-strings are null terminated ( ‘\0’ ) character arrays


Character array is a type of array whose components are of type char

Example:
‘A’ it is the character A
“A” it is the c-string A

 “A” represents 2 characters that is ‘A’ and ‘\0’

If we write the numbers in a set then the numbers in the set are is equal to no of elements
in array
But

If we use double quotation marks and simply write three numbers then it will give out error
because a null character takes part in it so we have to increase the number in the array by
one than number of elements
Figure 1 Error

‘\0’ :
It is used to find string length

Lab work:

Program No 1:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
double a;
double b;

cout<<&a<<endl;
cout<<&b;
return 0;
}

Figure 2 Output of program no 1

Program No 2:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
double a[5];

cout<<&a[0]<<endl;
cout<<&a[1];
return 0;
}

Figure 3 output of program no 2


Program No 3:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
char name[3]= {'1','2','3'};
cout<<name[2];
return 0;
}

Figure 4 Output of program no 3

Program No 4:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
char name[4]="123";
cout<<name[2];

return 0;
}
Figure 5 Output of Program no 4

Program No 5:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
char name[4]="123";
cout<<name[4];
return 0;
}

Figure 6 Output of Program no 5

Program No 6:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
char name[15]="Malik Haseeb";
cout<<name[4];
return 0;
}

Figure 7 Output of Program no 6

Program no 7:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
char name[19]="Malik Haseeb Ullah";
int a = 0;
while(name[a]!='\0')
{
a++;
}

cout<<"STring length is : "<<a;


return 0;
}
Figure 8 output of Program no 7

Program no 8:
#include<iostream>
using namespace std;
int main()
{ cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID 428302"<<endl;
char name[100];
cin.get(name,100);
int a= 0;

int spaces=0;
while(name[a]!='\0')
{
a++;
if(name[a]==' ')
{
spaces++;
}
}
cout<<"STring length is : "<<a-spaces;
a=0;
cout<<"\nFirst name : ";
while(name[a]!=' ')
{
cout<<name[a];
a++;

}
a++;
cout<<"\nSecond Name : ";
while(name[a]!=' ')
{
cout<<name[a];
a++;

cout<<"\nThird Name : ";


while(name[a]!='\0')
{
cout<<name[a];
a++;

}
return 0;
}
Figure 9 Output of Program no 8

Lab Task:
Program No 1:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID : 428302"<<endl;
string name;
getline(cin,name);
int a=0;

while(name[a]!=' ')
{
a++;
}
a++;
cout<<"Middle Name : ";
while(name[a]!=' ')
{
cout<<name[a];
a++;
if(name[a]=='\0')break;
}
}

Figure 10 Output of Task No 1

Program No 2:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID : 428302"<<endl;
int space =0;
string name;

cout<<"Name : ";
getline(cin,name);

string x;
int ch =0;
int length =0;
while(name[length]!='\0')
{
length++;
}

while(name[ch]!='\0')
{
if(space ==1) break;
if(name[ch]==' ')
{
for(int i=0; i<length; i++)
{
x += name[i+1+ch];
if (name[i+1+ch] == ' ')
{
space ++;
}
if(space ==1 || (i+1+ch)>=length) break;
}
}
ch++;
}
cout<<x; }

Figure 11 Output of task no 2


Program No 3:
#include<iostream>
using namespace std;
int main()
{
cout<<"Malik Haseeb Ullah"<<endl;
cout<<"CMS ID : 428302"<<endl;
cout<<"The real word is Its Our Style (IOS) "<<endl;
cout<<"After programming alphabets are changed"<<endl;
char str[20] ="Its Our Style (IOS)";

int i =0;
char F1st = str[0];
char a;
char E18 = str[17];
a = E18;
str[17] = F1st;
str[0] = a;
cout<<str;

Figure 12 Output of Task 3

You might also like