Professional Documents
Culture Documents
Object Oriented Programming Constructors and Destructors
Object Oriented Programming Constructors and Destructors
Example 1 :
#include<iostream.h>
void main( )
{
int a;
a = 50;
a = 100 ;
cout<<"\n a = "<<a ;
}
cin>>A[i];
cout<<"\n In Reverse Order : ";
for( i = SIZE - 1; i >= 0 ; i--)
cout<<"\t"<<A[i];
getch( );
}
#include<conio.h>
void show( int ) ;
void main( )
{
int number[ ]= { 11, 22, 33, 44, 55 } ;
for( int i = 0; i <= 4; i++)
show(number[i]); //passing element of an array to the function
getch( );
}
void show(int x)
{
cout<<"\t"<<x;
}
{
cout<<"\n Element "<< i <<"\t" ;
cout<<" Number "<< A[i] <<"\t" ;
cout<<" Address = "<<&A[i];
}
getch( );
}
j ++;
}
}
void main ()
{
int i , j ;
int digits[4][3] = { {0,1,2},{3,4,5},{6,7,8},{9,10,11} } ;
cout<<"\n The digits are :\n" ;
for (i=0 ; i<4 ; i++)
{
for (j=0 ; j<3 ; j++)
cout<<" \n "<<digits[i][j] ;
}
getch () ;
}
}
cout<<"The resulting 3X4 matrix is :\n" ;
for (i=0 ; i<3 ; i++)
{
for (j=0 ; j<4 ; j++)
cout<<multi[i][j]<<"\t";
cout <<"\n" ;
}
getch ( ) ;
}
auto int i= 2 ;
{
auto int i = 3;
cout<<"\n"<<i ;
}
cout<<"\n "<<i ;
}
cout<<"\n"<<i ;
}
{
if(m<n)
swap(m,n);
while (n > 0)
{
int r = m % n;
m = n;
n=r;
}
return m;
}
long LCM(long m, long n)
{
return m * n/GCD(m, n);
}
Example 27 : Program to perform sorting and to display largest and second largest
elements in the list
# include <iostream.h>
# include<conio.h>
# define TRUE 1
#define FALSE 0
#define MAX 20
void bubble(int x[ ], int n)
{
int hold, j, pass;
int switched = TRUE;
for(pass=0; pass<n-1&&switched==TRUE; pass++)
{ // Outer loop controls the number of passes
switched = FALSE;
// Initially no interchange have been made on this pass
for(j=0; j<n-pass-1; j++)
// Inner loop governs each individual pass
if (x[j] > x[j+1])
{
//Interchange elements if they are out of order
switched = TRUE;
hold=x[j];
x[j] = x[j+1];
x[j+1] = hold;
}
//end if
}
// end for
}
// end bubble function
void main( )
{
int i, n;
int x[MAX];
cout<<"\n Enter numbers of elements :";
cin>>n;
cout<<"\n Enter Numbers in any order : \n";
for(i=0; i<n; i++)
{
cout<<"\n Element["<<i<<"] = ";
cin>>x[i];
}
bubble(x,n);
cout<<"\n \nSorted Elements are : \n";
for(i=0; i<n; i++)
cout<<"\n Element["<<i<<"] = "<<x[i];
// Displays the largest and 2nd largest element from sorted list
cout<<"\n\n Largest Element is : "<<x[n-1];
cout<<"\n Second largest Element is : "<<x[n-2];
getch( );
}
Example 28 :
# include <iostream.h>
#include<math.h>
int sq_root (int);
double sq_root (double);
long int sq_root (long int);
void main (void)
{
int a = 25;
long int b = 225;
double c=225;
cout << "Square root of int value=" << sq_root (a);
cout << "Square root of double value =" <<sq_root (c);
cout << "Square root of long int value =" <<sq_root (b);
}
int sq_root (int a)
{
return (sqrt (a));
}
double sq_root (double c)
{
double x;
x = sqrt(c);
return (x);
}
long int sq_root(long int x)
{
long int z;
z = sqrt (x);
return (z);
}
Example 29 :
#include<iostream.h>
#include<conio.h>
#include<math.h>
double power(double m, int n = 2);
void main()
{
int m,n;
cout << "\n Enter value of m & n";
cin >> m >> n;
cout << " power("<< m <<", "<< n <<") = "
<< power (m, n);
cout << "power(" << m <<", 2)" << power (m);
getch( );
}
double power (double m, int n)
{
return(pow(m, n));
}
Example 30 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
float ADD (float, float);
int ADD (int, int);
char * ADD (char *, char *);
char S1 [50] , S2 [50], S3 [100];
void main ( )
}
float ADD (float a, float b)
{
return (a + b);
}
int ADD (int a, int b)
{
return (a + b);
}
char * ADD (char* S1, char* S2)
{
strcat (S1, S2) ;
return S1;
}
Example 31 :
//main file funcall.cpp
#include<conio.h>
#include<iostream.h>
#include "log_x10.cpp"
#include"expo.cpp"
void main()
{
logx10();
expo();
}
//logx_10.cpp
# include<math.h>
# include<iostream.h>
void logx_10()
{
int n=0;
cout<<"Enter number;";
cin>>n;
cout<<"The log value is "<<log(n);
}
//expo.cpp
# include<math.h>
# include<iostream.h>
void expo()
{
int n=0;
cout<<"Enter number:";
cin>>n;
cout<<"\n Exponential value="<<exp(n);
}
Example 32 :
# include<iostream.h>
# include<conio.h>
void main()
{
int j=0;
clrscr();
void extend(double[], double[],double[]);
double qty[10]={3,9.7,6.40,4.5,5.6,6.2,7,2.8,15.0,18.0};
double price[10]={10.20,11.30,13.14,16.9,18.1,2.71,7.55,15.12,9.45,17.0};
double amount[10]={1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0};
double tamount[10];
extend(qty,price,amount);
for (j = 0; j <= 9; j++)
{
tamount[j] = amount[j];
}
for(j=0;j<=9;j++)
{
cout<<tamount[j]<<"\n";
}
getch();
}
void extend(double q[], double p[], double amount[])
{
int i=0;
for(i=0;i<=9;i++)
{
amount[i]=q[i]*p[i];
}
}
Example 33 :
# include<iostream.h>
# include<conio.h>
void main()
{
int no=0,pos=0;
void delchar(char[],int,int);
char string[10];
cout<<"Enter string";
cin>>string;
cout<<"Enter no. of characters to delete";
cin>>no;
cout<<"Enter starting position";
cin>>pos;
pos = pos - 1;
delchar(string,no,pos);
getch();
}
void delchar(char s[], int n, int p)
{
int i, c;
for(i=p,c=0;c<n;i++)
{
s[i]=' ' ; c++;
}
while (s[i]!='\0')
{
s[p] = s[i];
p++; i++;
}
s[p]='\0';
cout<<"\nNow string is :" << s;
}
Example 34 :
#include<iostream.h>
#include<conio.h>
void main ( )
{
char a [20], ch ;
int i , count ;
cout<< "\n Enter a String : " ;
cin>>a;
cout<<"\nEnter a character to check its frequency in given string :";
cin>>ch;
i=0;
while (a[i]!='\0')
{
if ( ch == a [ i ] )
count = count +1 ;
i=i+1;
}
cout<< "\nFrequency of given letter is "<<count ;
getch ( ) ;
}
{
if ( strcmp ( p, s2 ) == 0 )
cout << "\n string found " ;
p = strtok ( NULL, " " ) ;
}
getch ( ) ;
}
Example 36 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main ( )
{
char s [ 15 ] ;
int i , p ;
cout<< "\nEnter any string : ";
cin>>s;
p = strlen ( s ) ;
for ( i = 0; i < p ; i ++ )
cout<< "\nElement at position "<<i+1<<" is"<< s[i] ;
getch ( ) ;
}
Example 37 :
#include<iostream.h>
#include<conio.h>
void main()
{
int x[100],i=1,avg=0,max=0,min=0,sum=0;
cout<<"\nEnter the values in the array X";
for(i=0; i<100; i++)
cin>>x[i];
for(i=0;i<100;i++)
cout << "Element ["<< i + 1 <<" ] = " << x[i];
for(i=0;i<100;i++)
sum=sum+x[i];
cout<<"\n Sum of array elements is : "<<sum;
avg=sum/100;
cout<<"\n Average of array elements is : "<<avg;
for(i=0; i<100; i++)
{
if (x[i]>avg) max++;
else min++;
}
cout<<"\n Total "<<max<<" numbers are present in array X ";
cout<<"\nwhich are greater than average";
cout<<"\nTotal "<<min<<" numbers are present in array X ";
cout<<"\nwhich are less than average";
getch( );
}
Example 38 :
#include<iostream.h>
#include<math.h>
#define MAX 5
#include<conio.h>
void main( )
{
float mean=0,variance=0;
int i,x[MAX];
clrscr();
for(i=0;i<MAX;i++)
{
cout<<"\n Enter array elements: ";
cin>>x[i] ;
mean = mean+x[i];
}
mean = mean/MAX;
cout<<"\n Mean : "<<mean ;
for(i=0;i<MAX;i++)
variance = variance + (x[i]-mean)*(x[i]-mean);
variance = variance/MAX;
cout<<"\nVariance : "<<variance ;
cout<<"\n Standard Deviation : "<<sqrt(variance);
}
Example 39 :
#include <iostream.h>
#include <math.h>
#include<conio.h>
int ack (int m, int n);
void main ( )
{
int x, y, z;
cout<< "\n Enter values of x and y" ;
cin>>x>>y;
z = ack (x, y);
cout<<z;
getch ( );
}
int ack ( int m, int n )
{
int p;
if ( m == 0 )
return ( n + 1 );
else if ( n == 0 && m > 0 )
p = ack ( m - 1, 1 );
else if ( m >= 0 && n >= 0 )
p = ack ( m - 1, ack ( m, n - 1 ) );
return (p);
}
Example 40 : Program to compute combination Function
# include <iostream.h>
# include<conio.h>
long comb( int n, int r);
void main( )
{
for ( int i = -1; i < 6; i++)
{
for( int j = -1; j <= i+1; j++)
cout<<" "<<comb(i,j);
cout<<endl;
}
}
long fact( int n);
long comb( int n, int r)
{
if ( n < 0 || r < 0 || r > n)
return 0;
return fact(n) / ( fact (r) * fact (n -r));
}
long fact (int n)
{
if (n < 2) return 1;
long f = 1;
for (int i = 2; i <= n; i++)
f *= i;
return f;
}
Example 41 :
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct sirname
{
char name[30];
}sn[10];
void main()
{
int i,j;
char temp[30];
clrscr();
for(i=0;i<5;i++)
{
cout<<"\nEnter "<<i+1<<" Sirname :";
cin>>sn[i].name;
}
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if(strcmp(sn[i].name,sn[j].name)>0)
{
strcpy(temp,sn[i].name);
strcpy(sn[i].name,sn[j].name);
strcpy(sn[j].name,temp);
}
}
}
for(i=0;i<5;i++)
cout<<"\n"<<sn[i].name;
getch();
}
Example 42 :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5
int main()
{
int a[MAX][MAX],i,j;
cout<<"\nEnter the matrix row wise :";
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
{
cout<<"\n Enter a["<<i<<"]["<<j<<"]element : ";
cin>>a[i][j];
}
}
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
{
if(i!=j && a[i][j]!=0)
{
cout<<"\nMatrix is not diagonal matrix";
getch(); exit(0);
}
}
}
cout<<"\nMatrix is diagonal matrix";
getch();
return(0);
}
{
cout << volume (10) <<endl;
cout <<volume (2.5, 8) <<endl;
cout <<volume (100L, 75, 15) <<endl;
getch( );
return 0;
}
int volume (int x)
// volume of cube
{
return (x*x*x);
}
double volume(double r, int h)
// volume of cylinder
{
return (3.14519*r*r*h) ;
}
long volume(long l, int b, int h)
// volume of rectangular
{
return (l*b*h);
}
Example 49 :
#include<iostream.h>
void function( int b)
{
b = 100 ;
cout<<"\n value :" <<b;
}
void main( )
{
int a = 50;
function(a);
cout<<"\n=value : "<<a;
}
#include<iostream.h>
void fun ( )
{
cout<<"\n Welcome to the world of C++ ";
}
int main( )
{
cout<<"\n Welcome to the world of Programming..";
fun ( );
return 0;
}
{
return (a*a);
}
void main(void)
{
cout << "Square(10) = " << square(10);
cout << "\n Square(10+10) = " << square(10+10);
getch( );
}
void main()
{
char c,ans;
int a;
do
{
cout<<"\nEnter a character -";
cin >> c;
if(isalnum(c)>0)
cout<<"\nThe character is an alphanumeric character.";
else
cout<<"The character is not an alphanumeric character.";
cout<<"\nDo you want to continue ? (y or n) : \n " ;
cin>>ans;
}
while((ans=='y')||(ans=='Y'));
}
#include <conio.h>
void main ( )
{
char c , ans ;
int a ;
do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (isalpha (c) > 0)
cout<<"\n The character is an alphabet." ;
else
cout<<"\n The character is not an alphabet." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}
void main ()
{
char c , ans ;
int a ;
do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (iscntrl (c) > 0)
cout<<"\n The character is a control character ." ;
else
cout<<"\n The character is not a control character." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}
do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (isdigit (c) > 0)
cout<<"\n The character is digit ." ;
else
cout<<"\n The character is not a digit ." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}
void main ( )
{
char c , ans ;
int a ;
do
{
cout<<"\n Enter a character -" ;
cin >>c ;
if (islower (c) > 0)
cout<<"\n The character is a lower case character ." ;
else
cout<<"\n The character is not a lower case character." ;
cout<<"\n Do you want to continue ? (y or n) : ";
cin >> ans;
}
while ( (ans == 'y') || (ans == 'Y') ) ;
getch();
}
Example 60 : Program to illustrate toupper( ) function
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
// header file for toupper( )
void main ( )
{
int i , character ;
cout<<"Lower Case Series \t Upper Case Series \n" ;
for ( i = 481 ; i < 507 ; i++)
{
character = toascii (i) ;
cout<<"\t"<< char(character) <<"\t\t\t"
<< char (toupper(character));
cout<<"\n";
}
getch ( ) ;
}
double i = 0.5 ;
result = atan ( i ) ;
cout<<"\n The arc tangent of "<< i << " is "<< result;
getch () ;
}
void main ( )
{
double number = 29.92 ;
double result ;
result = ceil ( number ) ;
cout<< "\n Original number = "<< number ;
cout<< "\n Number rounded up = "<< result ;
getch () ;
}
void main ( )
{
float i = -20.56;
cout.precision(2);
cout<<" i = "<<i;
cout<<"\n Absolute value of i = "<<fabs(i);
i = 20.56 ;
cout<<" i = "<<i;
cout<<"\n Absolute value of i = "<<fabs(i);
getch () ;
void main ( )
{
double result;
double x = 2.0 ;
result = exp ( x ) ;
cout<< "\n The result is : "<< result ;
getch ( ) ;
}
result = log ( x ) ;
cout<<" \n The natural log of "<< x <<" is "<<result ;
getch () ;
}
Example 70 : Program to illustrate the log10 ( ) function
#include <iostream.h>
#include <math.h>
// defines the log10 ( ) function
#include <conio.h>
void main ( )
{
double result;
double x = 10 ;
result = log10( x ) ;
cout<<" \n The natural log of "<< x <<" is "<<result ;
getch () ;
}
getch ( ) ;
}
Example 73 : Program to illustrate the atoi( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h >
// defines the atoi( ) function
void main()
{
int i ;
char *string = "145.97" ;
i = atoi ( string ) ;
cout<< "string = "<<string<<"\t integer = "<<i;
getch ( ) ;
}
char *string ;
itoa ( i, string, 10 ) ;
cout<<"integer = "<<i<<"\t string = "<<string;
getch ( ) ;
}
Example 76 : Program to illustrate the labs( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the labs( ) function
void main ( )
{
long i ;
i = -12345678L ;
cout<<"\n i = "<<i ;
cout<<"\n Absolute value of i = "<<labs(i);
i = 12345678L ;
cout<<"\n\n i = "<<i ;
cout<<"\n Absolute value of i = "<<labs(i);
getch() ;
}
#include<stdlib.h>
// defines the max( ) function
void main()
{
int x = 5;
int y = 6;
int z;
z=max(x,y);
cout<<"\nThe maximum number is "<<z ;
getch() ;
}
Example 79 : Program to illustrate the min( ) function
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
// defines the min( ) function
void main ( )
{
int x = 1, y = 7, z ;
z=min(x,y) ;
cout<<"\nThe minimum number is "<<z ;
getch();
}
void main( )
{
int i = 5 ;
int * j ;
// Declaration of Pointer Variable
j = &i ;
// Assigning address of variable i to variable j
cout <<" \n Address of i = " << &i ;
cout <<" \n Address of i = " << j ;
cout <<" \n Value of i = " << i ;
cout <<" \n Value of i = " << *(&i) ;
cout <<" \n Value of i = " << *j ;
}
#endif
getch () ;
}
#define m 2
void main()
{
clrscr () ;
#if m == 1
cout << " 'm' is One" ;
#elif m == 2
cout << " 'm' is Two" ;
#elif m == 3 || m == 4 || m == 5
cout << " 'm' is Between Three and Five" ;
#elif m >= 6 && m <= 10
cout << " 'm' is between Six and Ten" ;
#else
cout << " 'm' is not in the range [1-10]" ;
#endif
getch () ;
}
Example 90 : Program for Use of '#if' as '#ifdef'
#include <iostream.h>
#include <conio.h>
#define print 1
/* macro 'print' definition */
void main()
{
int a = 10 ;
clrscr () ;
a=a+1;
#if defined (print) /* if macro 'print' defined */
cout << "a = " << a ;
#endif
getch () ;
}
a[i] = i ;
xyz ;
/* use of call to a macro */
for ( i = 0; i < 5; i++ )
a[i] -= 1 ;
xyz ;
/* use of call to a macro */
getch () ;
}
Example 103 : Program that determines whether the given integer is a square
number
# include <iostream.h>
# include<conio.h>
int isSquare (int);
int main( )
{
const int MAX = 20;
# include<conio.h>
int sum (int[ ], int);
float avg (int[ ], int);
void main( )
{
int a[ ]={11,33,55,77};
int size = sizeof(a) /sizeof(int);
cout<<"Sum (a, size) = "<<sum(a,size)<<endl;
cout<<"Average (a, size) = "<<avg(a,size)<<endl;
}
int sum(int a[ ], int n)
{
int sum1 = 0;
for(int i = 0; i < n; i++)
sum1= sum1+a[i] ;
return sum1;
}
float avg (int a[ ], int n)
{
float sum = 0.0;
for(int i = 0; i < n; i++)
sum= sum +a[i] ;
return sum/n;
}
Example 106 : Program to search a given integer in an array of integers
# include <iostream.h>
# include<conio.h>
int search(int[], int);
void main( )
{
int arr[10], num, i, subscript;
cout<<"\n Input 5 array elements : ";
for(i=0;i<5;i++)
cin>>arr[i];
cout<<"\n Enter number to be searched :";
cin>>num;
if((subscript = search(arr,num)) == -1)
cout<<"\n No such element !! ";
else
cout<<"\n Element found at : "<<subscript;
getch( );
}
int search(int arr[], int num)
{
int i ;
for(i=0; i<5; i++)
{
if (arr[i] == num)
return i +1;
}
return -1;
}
if (result == 0)
cout<<"\n No match found";
else if(result != 0)
{
cout<<"\n Match found \n";
cout<< "Position of number "<< number<<" is : "<<mid +1;
}
}
for (i=0;i<3;i++)
for (j=0;j<3;j++)
cin>>matrix[i][j];
for (i=0; i<3; i++)
// copy into an array for sorting
for (j=0; j<3; j++)
sort[k++] = matrix[i][j];
for (i=0; i<9; i++)
//sort the copied array
for (j=i+1; j<9; j++)
{
if (sort[i] > sort[j])
{
temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
cout<<"\n Sorted matrix is : \n ";
for ( k=0, i=0, j=0; k<9; k++)
//copy back to the matrix
{
if (j == 3)
{
j = 0;
i ++;
cout<<"\n";
}
matrix[i][j] = sort[k];
cout << matrix[i][j];
//print sorted matrix.
j++;
}
getch( );
}
Example 110 : Program to count number of times the word 'the' appears in the
string
# include <iostream.h>
# include<conio.h>
#include<string.h>
#include<stdio.h>
void main( )
{
char s[20], s1[20], *p = " ";
int i , count = 0;
cout<<"\n Input the String : \n";
gets(s);
strcpy(s1,s);
p = strtok(s1," ") ;
// get the first word
for( i = 0 ;p;i++)
{
if (strcmp (p,"the") == 0 )
count++;
p = strtok(NULL," " );
//get the next word
}
cout<<"\n Count is : "<<count ;
}
Example 113 : Program to accept a 3 digit number and print it out digit by digit as a
series of words.
# include <iostream.h>
# include<conio.h>
void main( )
{
int n;
char s[10][6]={ "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine"};
cout<<"\n Enter 3 digit number : ";
cin >> n;
cout<< "\n The Number is : " <<n ;
cout<<"\n"<<s[n/100];
n = n % 100;
cout<<" "<<s[n/10];
n = n % 10;
cout<<" "<<s[n/1];
getch( );
}
Example 114 : Program which concatenates a string to the left of a given string
# include <iostream.h>
# include<conio.h>
#include<string.h>
void main( )
{
char *s1="Good", *s2= "Day", *s3;
s3 = s2 + (strlen(s2)-1); // s3 points to last character of s2
while(*s2 != '\0')
{
*--s1=*s3--;
s2++;
}
cout<<s1;
//print concatenated string
getch( );
}
//Declaration (Prototype)
void main( )
{
int a, fact;
cout<<"\n Enter any Number : " ;
cin>>a;
if ( a < 0)
cout<<"\n Number should be positive !! ";
else
{
fact = recur ( a ) ;
cout<<"\n Factorial value : " << fact ;
}
getch( );
}
int recur ( int n)
{
if ( n == 1 )
return (1);
else
return (n * recur ( n - 1 ) );
}
swap(a, b);
cout <<"\n After swap a = "<<a<<"and b = "<<b;
}
void swap(int& x, int& y)
{
//exchange the values of x and y
int temp = x;
x = y;
y = temp;
}
Example 119 : Program to illustrates Static Storage Classes
#include<iostream.h>
void incre( ) ;
void main( )
{
incre( );
incre( );
incre( );
incre( );
}
void incre( )
{
static int i = 1;
cout << "\n" <<i ;
i = i + 1;
}
void main ( )
{
char source [] = "Hello, Have a Nice Day" ;
char *p;
cout<< "\n Source String is : "<<source ;
/* strtok( ) places a NULL terminator
in front of the token, if found */
p = strtok ( source, "," ) ;
if ( p )
cout<< "\n First Token : "<<p;
/* A second call to strtok( ) using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok ( NULL, " " ) ;
if ( p )
cout<< "\n Second Token : "<<p;
/* Similarly other tokens can be obtained */
p = strtok ( NULL, " " ) ;
if ( p )
cout<< "\n Third Token : "<<p;
getch ( ) ;
}
{
int count = 0 ;
while (line[count] != '\0')
count++ ;
return (count) ;
}
Example 135 : Program to Copy a String
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void main ( )
{
int i ;
char sentence[40] , copy[40] ;
cout<<"Enter any string :" ;
gets (sentence) ;
i=0;
for (i=0 ; i<40 ; i++)
copy[i] = 0 ;
i=0;
while (sentence[i] != '\0')
{
copy[i] = sentence[i] ;
i++ ;
}
cout<<"\nThe copied string is : " ;
puts (copy) ;
getch ( ) ;
}
Example 136 : Program to perform Concatenation of Strings
#include <iostream.h>
#include <conio.h>
#include<stdio.h>
void concatenate (char str1[40],char str2[40]) ;
void main ()
{
char str1[60] , str2[40] ;
puts ("Enter the first string :") ;
gets (str1) ;
puts ("\nEnter the second string :") ;
gets (str2) ;
concatenate (str1,str2) ;
puts ("\nThe concatenated string is :") ;
puts (str1) ;
getch ( ) ;
}
void concatenate (char str1[40], char str2[40])
{
int i, j;
i=0;
while (str1[i] != '\0')
i++ ;
str1[i] = ' ' ;
i++ ;
j=0;
while (str2[j] != '\0')
{
str1[i] = str2[j] ;
i++ ;
j++ ;
}
}
if ( (str1[count] == '\0')&&(str2[count]=='\0'))
puts ("\nThe two strings are equal.") ;
else
puts ("\nThe two strings are unequal.") ;
getch ( ) ;
}
int compare (int i,char str1[],char str2[])
{
i=0;
while ((str1[i]==str2[i])&&(str1[i]!='\0')&&(str2!='\0'))
++i ;
return (i) ;
}
Example 139 : Program to illustrate Difference between Call by Value & Call by
Reference
#include <iostream.h>
#include <conio.h>
void f (int, int &);
void main( )
{
int a = 22, b = 44;
cout<<" a = "<<a <<",
f(a,b);
cout<<" a = "<<a <<",
f(2*a-3,b);
cout<<" a = "<<a <<",
getch();
}
void f(int x, int *y)
{
x = 88;
* y = 99;
}
b = " <<b<<endl;
b = " <<b<<endl;
b = " <<b<<endl;