In C write a menu driven program to write your own string
handling functions. Don’t use
static arrays, ONLY dynamic memory allocation is allowed.
1. Convert a string to integer.
2. Concatenate two strings
3. Copy string to another string
4. Compare two strings
5. String length
6. Quit
#include <stdio.h>
#include <string.h>
void convertToString() // convert string to int
{
char str[100];
int x;
printf("Please enter the string: ");
scanf("%s", str);
sscanf(str, "%d", &x);
printf("\nThe value of x : %d", x);
}
void concatenate() // concatanate two strings
{
char str1[100], str2[100];
printf("Please enter the two strings: ");
scanf("%s", str1);
scanf("%s", str2);
strcat(str1, str2);
printf("String obtained on concatenation: %s\n", str1);
}
void copyStrings() // copies one string to another string
{
char str1[100], str2[100];
printf("Please enter the string: ");
scanf("%s", str1);
strcpy(str2, str1);
printf("After copying: %s\n", str2);
}
void comparing() // comparing two string if they are equal or
not
{
char str1[100], str2[100];
printf("Please enter the two strings: ");
scanf("%s", str1);
scanf("%s", str2);
if(strcmp(str1, str2) == 0)
{
printf("Hence both are equal");
}
else
{
printf("Both are not equal");
}
}
void stringLength() // prints string length
{
char str1[100];
printf("Please enter the two strings: ");
scanf("%s", str1);
printf("String length is : %d", strlen(str1));
}
int main()
{
do{
int n;
char str[1000];
int x;
printf("\n");
printf("1. Convert string to integer\n");
printf("2. Concatenate two strings\n");
printf("3. Copy string to another string\n");
printf("4. Compare two strings\n");
printf("5. String Length\n");
printf("6. Quit\n");
printf("please enter your choice: ");
scanf("%d",&n);
printf("\n");
switch(n)
{
case 1: convertToString(); printf("\n"); break;
case 2: concatenate(); printf("\n"); break;
case 3: copyStrings(); printf("\n"); break;
case 4: comparing(); printf("\n"); break;
case 5: stringLength(); printf("\n"); break;
default : exit(0);
}
}while(1);
return 0;
}
OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.