Sunteți pe pagina 1din 27

QUES- WRITE A MENU DRIVEN PROGRAM TO PERFORM THE STRING HANDLING OPERATIONS WITHOUT

USING INBUILT FUNCTIONS

//Header Files
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<string.h>

void main() //Start of main


{
clrscr();
//Declaration of variables
char choice, symbol,s1[10],s2[10],s3[20];
int i,j,count=0,r1,r2,flag,l1,l2;
//Start of do while loop
do
{ //Main Menu
cout<<"\t MAIN MENU \n";
cout<<"1. LENGTH OF A STRING \n";
cout<<"2. COPY A STRING \n";
cout<<"3. REVERSE A STRING \n";
cout<<"4. CONCATINATE TWO STRINGS \n";
cout<<"5. COMPARE TWO STRINGS \n";
cout<<"6. EXIT \n";

cout<<"Enter your choice : ";


cin>>symbol;

switch(symbol) //Start of switch case


{
//Finding length of a string
case '1': cout<<"Enter the string\n";
cin>>s1;
for(i=0;s1[i]!='\0';i++)
count++;
cout<<"The length of string is "<<count;
break;

//Copying one string into another


case '2': cout<<"Enter the string\n";
cin>>s1;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
s2[i]='\0';
cout<<"The copied string is "<<s2;
break;

//Finding the reverse of a string


case '3': cout<<"Enter the string\n";
cin>>s1;
for(i=0;s1[i]!='\0';i++)
count++;

for(i=count-1,j=0;i>=0;i--,j++)
{
s2[i]=s1[j];
}
s2[j]='\0';

cout<<"The reversed string is "<<s2;


break;

//Concatinating two strings


case '4': cout<<"Enter the first string\n";
cin>>s1;
cout<<"Enter the second string\n";
cin>>s2;
for(i=0,j=0;s1[i]!='\0';i++,j++)
s3[j]=s1[i];

s3[j]=' ';

j++;

for(i=0;s2[i]!='\0';i++,j++)
s3[j]=s2[i];

s3[j]='\0';
cout<<"The concatinated string is "<<s3;
break;

//Comparing two strings


case '5': cout<<"Enter the first string\n";
cin>>s1;
cout<<"Enter the second string\n";
cin>>s2;

l1=strlen(s1);
l2=strlen(s2);
if(l1!=l2)
cout<<"The two strings are unequal";
else
{
for(i=0;s1[i]!='\0';i++)
{
r1=s1[i];
r2=s2[i];
if(r1!=r2)
{
flag=1;
break;
}
}
}
if(flag==1)
cout<<"The strings are not equal";
else
cout<<"The strings are equal";
break;

//Exit the program


case '6': exit(0);

//Wrong choice
default: cout<<"Invalid entry";

} //End of switch case

cout<<"\nDo you want to continue?\n";


cin>>choice;

}while(choice=='y'||choice=='Y'); //End of do while loop

}//End of main

/*
OUTPUT

MAIN MENU
1. LENGTH OF A STRING
2. COPY A STRING
3. REVERSE A STRING
4. CONCATINATE TWO STRINGS
5. COMPARE TWO STRINGS
6. EXIT

CASE 1

Enter your choice : 1


Enter the string
prateek
The length of string is 7
Do you want to continue?
N
CASE 2

Enter your choice : 2


Enter the string
prateek
The copied string is prateek
Do you want to continue?
N

CASE 3

Enter your choice : 3


Enter the string
prateek
The reversed string is keetarp
Do you want to continue?
N

CASE 4

Enter your choice : 4


Enter the first string
prateek
Enter the second string
agrawal
The concatinated string is prateek agrawal
Do you want to continue?
N

CASE 5

Enter your choice : 5


Enter the first string
prateek
Enter the second string
sambhav
The strings are not equal
Do you want to continue?
N

CASE 6

Enter your choice : 6

*/
QUES- WRITE A MENU DRIVEN PROGRAM WHICH CONVERTS VARIOUS DATA FORMS INTO EACH OTHER

//Header Files
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
#include<string.h>

//Start of main
void main()
{
clrscr();
// Declaration of variables
int bin[20],n,i=0,r,j,sum=0,term,num,c,length,k;
char symbol,choice,ch,s1[10];

//Start of do while loop


do
{
//Main Menu
cout<<"\t MAIN MENU \n";
cout<<"1. CONVERT DECIMAL NUMBER INTO BINARY\n";
cout<<"2. CONVERT BINARY NUMBER INTO DECIMAL\n";
cout<<"3. CONVERT DECIMAL NUMBER INTO OCTAL\n";
cout<<"4. CONVERT OCTAL NUMBER INTO DECIMAL\n";
cout<<"5. CONVERT DECIMAL NUMBER INTO HEXADECIMAL\n";
cout<<"6. CONVERT HEXADECIMAL NUMBER INTO DECIMAL\n";
cout<<"7. EXIT\n";

cout<<"Enter your choice : ";


cin>>symbol;

//Start of switch case


switch(symbol)
{
// Decimal number into binary
case '1': cout<<"Enter the decimal number ";
cin>>n;

while(n!=0)
{
r=n%2;
bin[i]=r;
i++;
n=n/2;
}
for(j=i-1;j>=0;j--)
cout<<bin[j];
break;

//Binary number into decimal


case '2': cout<<"Enter the binary number ";
cin>>n;

while(n!=0)
{
r=n%10;
term=r*pow(2,i);
i++;
sum=sum+term;
n=n/10;
}
cout<<"The decimal form is "<<sum;
break;

//Decimal into octal


case '3': cout<<"Enter the decimal number ";
cin>>n;

while(n!=0)
{
r=n%8;
bin[i]=r;
i++;
n=n/8;
}
for(j=i-1;j>=0;j--)
cout<<bin[j];
break;

//Octal into decimal


case '4': cout<<"Enter the octal number ";
cin>>n;

while(n!=0)
{
r=n%10;
term=r*pow(8,i);
i++;
sum=sum+term;
n=n/10;
}
cout<<"The decimal form is "<<sum;
break;

//Decimal into hexadecimal


case '5': cout<<"Enter the decimal number ";
cin>>n;

while(n!=0)
{
r=n%16;
bin[i]=r;
i++;
n=n/16;
}

for(j=i-1;j>=0;j--)
{
num=bin[j];
if(num==10)
cout<<'A';
else if(num==11)
cout<<'B';
else if(num==12)
cout<<'C';
else if(num==13)
cout<<'D';
else if(num==14)
cout<<'E';
else if(num==15)
cout<<'F';
else
cout<<num;
}

break;

//Hexadecimal into decimal


case '6': cout<<"Enter the hexadecimal number ";
cin>>s1;
length=strlen(s1);

for(i=length-1,c=0;i>=0;i--,c++)
{
if(s1[i]=='1')
k=1;
else if(s1[i]=='2')
k=2;
else if(s1[i]=='3')
k=3;
else if(s1[i]=='4')
k=4;
else if(s1[i]=='5')
k=5;
else if(s1[i]=='6')
k=6;
else if(s1[i]=='7')
k=7;
else if(s1[i]=='8')
k=8;
else if(s1[i]=='9')
k=9;
else if(s1[i]=='A'||s1[i]=='a')
k=10;
else if(s1[i]=='B'||s1[i]=='b')
k=11;
else if(s1[i]=='C'||s1[i]=='c')
k=12;
else if(s1[i]=='D'||s1[i]=='d')
k=13;
else if(s1[i]=='E'||s1[i]=='e')
k=14;
else if(s1[i]=='F'||s1[i]=='f')
k=15;

term=k*pow(16,c);
sum=sum+term;
}
cout<<"The decimal form is "<<sum;
break;

//Exit the program


case '7': exit(0);

//Wrong choice
default: cout<<"Invalid entry\n";

}//End of switch case

cout<<"\nDo you want to continue? \n";


cin>>choice;

}while(choice=='y'||choice=='Y'); //End of do while loop

}//End of main
/* OUTPUT

MAIN MENU
1. CONVERT DECIMAL NUMBER INTO BINARY
2. CONVERT BINARY NUMBER INTO DECIMAL
3. CONVERT DECIMAL NUMBER INTO OCTAL
4. CONVERT OCTAL NUMBER INTO DECIMAL
5. CONVERT DECIMAL NUMBER INTO HEXADECIMAL
6. CONVERT HEXADECIMAL NUMBER INTO DECIMAL
7. EXIT

CASE 1

Enter your choice : 1


Enter the decimal number 135
10000111
Do you want to continue?
n

CASE 2

Enter your choice : 2


Enter the binary number 10011
The decimal form is 19
Do you want to continue?
N

CASE 3

Enter your choice : 3


Enter the decimal number 157
235
Do you want to continue?
N

CASE 4

Enter your choice : 4


Enter the octal number 178
The decimal form is 128
Do you want to continue?
N

CASE 5

Enter your choice : 5


Enter the decimal number 109
6D
Do you want to continue?
N

CASE 6

Enter your choice : 6


Enter the hexadecimal number a13
The decimal form is 2579
Do you want to continue?
n

CASE 7

Enter your choice : 7

DEFAULT

Enter your choice : 8


Invalid entry

Do you want to continue?


N

*/
QUES- WRITE A MENU DRIVEN PROGRAM TO PERFORM THE STRING HANDLING OPERATIONS USING INBUILT
FUNCTIONS

//Header Files
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<process.h>

//start of main

void main()
{
clrscr();

//Decleration of variables

char s1[20], s2[20], s3[40];


char symbol, choice;
int length,compare;

//Start of do while loop

do
{
//Main menu
cout<<"\t MAIN MENU\n";
cout<<" 1. LENGTH OF A STRING \n";
cout<<" 2. COPY A STRING \n";
cout<<" 3. REVERSE OF A STRING \n";
cout<<" 4. CONCATINATE TWO STRINGS \n";
cout<<" 5. COMPARE TWO STRINGS (WITH CASE) \n";
cout<<" 6. COMPARE TWO STRINGS (WITHOUT CASE) \n";
cout<<" 7. EXIT \n";

cout<<"Enter your choice : ";


cin>>symbol;

//Start of switch case

switch(symbol)
{
//Finding length of a string
case '1': cout<<"Enter the string \n";
cin>>s1;
length=strlen(s1);
cout<<"The length of the string is "<<length;
break;
//Copying one string into another
case '2': cout<<"Enter the string \n";
cin>>s1;
strcpy(s2,s1);
cout<<"The copied string is "<<s2;
break;

//Finding the reverse of a string


case '3': cout<<"Enter the string \n";
cin>>s1;
strrev(s1);
cout<<"The reversed string is "<<s1;
break;

//Concatinating two strings


case '4': cout<<"Enter the first string \n";
cin>>s1;
cout<<"Enter the second string \n";
cin>>s2;
cout<<endl;
strcat(s3,s1);
strcat(s3," ");
strcat(s3,s2);
cout<<"The concatinated string is "<<s3;
break;

//Comparing two strings (case sensitive)


case '5': cout<<"Enter the first string \n";
cin>>s1;
cout<<endl;
cout<<"Enter the second string \n";
cin>>s2;
cout<<endl;
compare=strcmp(s1,s2);
if (compare==0)
cout<<"The two strings are equal";
else if (compare>0)
cout<<"The first string is greater than the second string";
else
cout<<"The second string is greater than the first string";
break;

//Comparing two strings (case insensitive)


case '6': cout<<"Enter the first string \n";
cin>>s1;
cout<<endl<<"Enter the second string \n";
cin>>s2;
cout<<endl;
compare=strcmpi(s1,s2);
if(compare==0)
cout<<"The two strings are equal \n";
else
cout<<"The two strings are unequal \n";
break;

//Exit the program


case '7': exit(0);

//Wrong choice
default: cout<<"Invalid entry \n";

}//End of switch case

cout<<"\nDo you want to continue? \n";


cin>>choice;

}while(choice=='y'||choice=='Y'); //End of do while loop


//End of do while loop
}//End of main

/* OUTPUT

MAIN MENU
1. LENGTH OF A STRING
2. COPY A STRING
3. REVERSE OF A STRING
4. CONCATINATE TWO STRINGS
5. COMPARE TWO STRINGS (WITH CASE)
6. COMPARE TWO STRINGS (WITHOUT CASE)
7. EXIT

CASE 1

Enter your choice : 1


Enter the string
Prateek
The length of the string is 7
Do you want to continue?
N

CASE 2

Enter your choice : 2


Enter the string
Prateek
The copied string is Prateek
Do you want to continue
N

CASE 3

Enter your choice : 3


Enter the string
Prateek
The reversed string is keetarP
Do you want to continue?
N

CASE 4

Enter your choice : 4


Enter the first string
Aman
Enter the second string
Gandhi

The concatinated string is Aman Gandhi


Do you want to continue?
N

CASE 5

Enter your choice : 5


Enter the first string
Prateek

Enter the second string


Sambhav

The second string is greater than the first string


Do you want to continue?
N

CASE 6

Enter your choice : 6


Enter the first string
Prateek

Enter the second string


Sambhav
The two strings are unequal

Do you want to continue?


N

CASE 7

Enter your choice : 7

DEFAULT

Enter your choice : 9


Invalid entry

Do you want to continue?


N

*/
QUES- WRITE A PROGRAM TO PRINT THE PATTERN

//Header files
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>

//Start of main
void main()
{
clrscr();
int i,s,j,n;
cout<<"Enter the number of stars in the pattern : ";
cin>>n;
cout<<endl;

for(i=1,s=n;i<=n;i++,s--)
{
cout<<setw(s)<<"";

for(j=1;j<=i;j++)
cout<<"* ";
cout<<"\n";
}
for(i=n-1,s=2;i>=1;i--,s++)
{
cout<<setw(s)<<"";

for(j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
}//End of main

/* OUTPUT
Enter the number of stars in the pattern : 5

*
**
***
****
*****
****
***
**
*
*/

S-ar putea să vă placă și