You are on page 1of 12

1.

PROGRAM TO DEMONSTRATE THE INITIALIZATION OF


ARRAYS

#include<iostream.h>
#include<conio.h>
void main()
{
int a[]={5,4,3,2,1};
int n;
for(n=0;n<5;n++)
cout<<”\n element in a[“<<n<<”] is: “<<a[n];
getch();
}

OUTPUT:

element in a[0] is : 5
element in a[1] is : 4
element in a[2] is : 3
element in a[3] is : 2
element in a[4] is : 1
2.PROGRAM TO INITIALIZE THE ARRAY ELEMENTS OF A
2-D ARRAY & DISPLAY THE CONTENT ON SCREEN

#include<iostream.h>
#include<conio.h>
void main()
{
int a[3][3]={1,2,3,4,5,6,7,8,9};
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<”\n the element in a[“<<i<<”][“<<j<<”] is:”<<a[i][j];
}
getch();
}

OUTPUT:
the element in a[0][0] is: 1
the element in a[0][1] is: 2
the element in a[0][2] is: 3
the element in a[1][0] is: 4
the element in a[1][1] is: 5
the element in a[1][2] is: 6
the element in a[2][0] is: 7
the element in a[2][1] is: 8
the element in a[2][2] is: 9

3.PROGRAM TO READ IN A MATRIX

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a[10][10],r,c,i,j;
cout<<"enter the no. of rows";
cin>>r;
cout<<"enter no. of columns";
cin>>c;
cout<<"\n enter elements";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cin>>a[i][j];
}
cout<<"matrix is:";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
getch();
}

OUTPUT:

enter no. of rows: 2


enter no. of columns:3
enter elements:4,5,6,7,8,9
matrix is :4 5 6
7 8 9

4. PROGRAM TO INITIALIZE A SET OF CHARACTERS &


DISPLAY THE CONTENTS OF GIVEN STRING

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char string[50]=”MY NAME IS RAHUL”;
cout<<”The content of initialized string is: “<<string;
getch();
}
OUTPUT:

The content of initialized string is: MY NAME IS POONAM

5.PROGRAM TO FIND THE LENGTH OF GIVEN STRING

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char string[40];
cout<<”please enter the string:”;
gets(string);
cout<<”the length of the given string is:”<<strlen(string);
getch();
}

OUTPUT:

please enter the string:HELLO FRIENDS


the length of the given string is:13

6.PROGRAM TO COPY THE CONTENTS OF 1 STRING TO


ANOTHER

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char string1[50],string2[50];
cout<<”enter 1st string:”;
gets(string1);
cout<<”enter 2nd string:”;
gets(string2);
cout<<”strings after copying:”;
strcpy(string1,string2);
cout<<”\n new string1:”<<string1;
cout<<”\n new string2:”<<string2;
getch();
}

OUTPUT:

enter 1st string: hello


enter 2nd string:friends
strings after copying:
new string1:friends
new string2:friends

7. PROGRAM TO COMBINE TWO STRINGS & DISPLAY THE


RESULT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
clrscr();
char s1[20],s2[20];
cout<<"enter 1st string:";
gets(s1);
cout<<"enter 2nd string:";
gets(s2);
strcat(s1,s2);
cout<<"string after combination/concatenation:"<<s1;
getch();
}

OUTPUT:
enter 1 string: good evening
st

enter 2nd string: everybody


string after combination/concatenation: good evening everybody
8. PROGRAM TO COMPARE TWO STRINGS
ALPHABETICALLY

//RETURN VALUE returns an integer value indicating results of comparison:


//<0 if string 1 is less than string 2.
//=0 if string 1 is equal to string 2.
//>0 if string 1 is greater than string 2.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s1[50],s2[50];
cout<<"enter 1st string";
cin>>s1;
cout<<"enter string 2";
cin>>s2;
int i=strcmp(s1,s2);
cout<<i;
getch();
}

OUTPUT:

enter 1st string:hello


enter 2nd string:hello
o
9.PROGRAM TO ACCESS A STRING & CONVERT THE
CHARACTERS FROM UPPER CASE TO LOWER CASE

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
int main()
{
clrscr();
char str1[50];
cout<<”please enter a string”;
cin>>str1;
strlwr(str1);
cout<<"the given string in lower case is:"<<str1<<endl;
getch();
}

OUTPUT:

please enter a string: HELLO FRIENDS


The given string in lower case is:
hello friends

10.PROGRAM TO CONVERT A STRING FROM LOWER CASE


TO UPPER CASE

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[50];
cout<<”please enter a string”;
cin>>str1;
strupr(str1);
cout<<"the given string in upper case is:"<<str1<<endl;
getch();
}
OUTPUT:

please enter a string: hello friends


The given string in upper case is:
HELLO FRIENDS

You might also like