Assignment No 10
Assignment No 10
Assignment No 10
Lab Report 10
Theory:
1. Address operators:
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:
Example:
‘A’ it is the character A
“A” it is the c-string A
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;
}
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;
}
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;
}
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;
}
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++;
}
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++;
}
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;
}
}
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; }
int i =0;
char F1st = str[0];
char a;
char E18 = str[17];
a = E18;
str[17] = F1st;
str[0] = a;
cout<<str;