How to write a menu driven program in C
1: calculate the length of string
2: copy one string into another
3: compare two string
4: concatenate of two string
Solution:
/* write a menu driven program
1: calculate the length of string
2: copy one string into another
3: compare two string
4: concatenate of two string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int ch,n;
clrscr();
printf("\n1:length of string:");
printf("\n2:copy string");
printf("\n3:compare string");
printf("\n4:concatenate two string");
printf("\nenter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter the string:");
flushall();
gets(a);
n=strlen(a);
printf("\nthe lenght of string is :%d",n);
break;
case 2:printf("\nenter the firs string :");
flushall();
scanf("%s",&a);
strcpy(b,a);
printf("the copied string is :%s",b);
break;
case 3:printf("\nenter first string :");
flushall();
scanf("%s",&a);
printf("\nenter second string :");
flushall();
scanf("%s",&b);
if(strcmp(a,b)>0)
printf("\nstring a is greater");
else
printf("\nstring b is greater");
break;
case 4:printf("\nenter first string :");
flushall();
scanf("%s",&a);
printf("\nenter second string :");
flushall();
scanf("%s",&b);
strcat(a,b);
printf("\nthe source string is :%s",b);
printf("\nthe target string is : %s",a);
break;
}
getch();
}
/*
1:length of string:
2:copy string
3:compare string
4:concatenate two string
enter your choice :1
enter the string:peer
the lenght of string is :4
1:length of string:
2:copy string
3:compare string
4:concatenate two string
enter your choice :2
enter the firs string :peersaab
the copied string is :peersaab
1:length of string:
2:copy string
3:compare string
4:concatenate two string
enter your choice :3
enter first string :peersaab
enter second string :nadaf
string a is greater
1:length of string:
2:copy string
3:compare string
4:concatenate two string
enter your choice :4
enter first string :peer
enter second string :saab
the source string is :saab
the target string is : peersaab
*/