Problem solving and programming
Problem solving and programming
Roll No.: 09
PRN: 12412991
Class: CSDS-A (FY)
Assignment 5
Write a Menu Driven Program to Accept String from the user. Perform the following operations on the
String (Without Using Built in Functions):
c. Copy given string into other string and print both strings.
d. Accept two strings from user and compare both. Print if they are equal. Or which one is greater or
which one is small.
e. Accept two strings from user and print its concatenated string.
Code:
#include<stdio.h>
int main()
int result=0;
int i=0;
while (1)
printf("3. Copy given string into other string and print both strings.\n");
printf("4. Accept two strings from user and compare both the strings. Print if they are equal. Or
which one is greater or which one is small.\n");
printf("5. Accept two strings from user and print its concatenated string.\n");
scanf ("%d",&choice);
getchar();
switch(choice)
case 1:
str1[strlen(str1)-1]= '\0';
len=0;
while(str1[len] != '\0')
len++;
break;
case 2:
str1[strlen(str1)-1]= '\0';
len=0;
len ++;
start=0;
end=len-1;
while(start<end)
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start++;
end--;
break;
case 3:
fgets(str1,sizeof(str1),stdin);
str1[strlen(str1)-1]='\0';
str2[strlen(str2)-1]='\0';
i=0;
while(str1[i] !='\0')
str2[i]=str1[i];
i++;
str2[i]='\0';
break;
case 4:
printf("Enter the first string: ");
fgets(str2,sizeof(str2),stdin);
str2[strlen(str2)-1]='\0';
i=0;
flag=0;
while(str1[i]!='\0'||str2[i]!='\0')
if (str1[i]!=str2[i])
flag=1;
break;
i++;
if (flag==0)
else
if (str1[i]=='\0'&&str2[i]!='\0')
else if(str1[i]!='\0'&&str2[i]=='\0')
else
break;
case 5:
fgets(str1, sizeof(str1),stdin);
str1[strlen(str1)-1]='\0';
fgets(str2, sizeof(str2),stdin);
str2[strlen(str2)-1]='\0';
i=0;
while(str2[i] !='\0')
i++;
j=0;
while(str2[j]!='\0')
str1[i]=str2[j];
i++;
j++;
str1[i]='\0';
break;
case 6:
default:
return 0;
Algorithm:
2: Reverse a string
3: Copy a string
3. Swap characters from start and end until the middle is reached.
2. Copy each character from the source string to the destination string.
4. Otherwise, determine which one is greater or smaller and display the result.
1 choice = 1 - -
2 str1 = "JOHN" - -
3 len = 0 len = 0 -
5 len++ len = 1 -
7 len++ len = 2 -
9 len++ len = 3 -
11 len++ len = 4 -
1 choice = 2 - - - -
3 len = 4 - - - "GOOD"
1 choice = 3 - - -
3 i=0 i=0 - -
1 choice = 4 - - -
2 str1 = "ASHRAF" - - -
3 str2 = "AHMED" - - -
"A" == "A" →
4 `while (str1[i] != '\0' str2[i] != '\0')`
Continue
"S" != "H" →
5 i++ i=1 Strings not equal
Break
1 choice = 5 - - - -
Step Code Execution i j Condition Check str1
printf("Concatenated
Output:
10 string:
"EXCELLENTWORK"
EXCELLENTWORK")
Case 6: Exit
1 choice = 6 6
OUTPUT:
Flow Chart: