Name: Ashraf Ahmed
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):
a. Print String Length
b. Print String in reverse order
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>
#include<string.h> //For strlen funtion
int main()
int choice, flag =0;
char str1[100], str2[100];
int j,len, start, end,temp;
int result=0;
int i=0;
while (1)
printf("\n Menu: \n");
printf("1. Print String Length \n");
printf("2. Print String in Reverse Order \n");
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");
printf("6. Exit \n");
scanf ("%d",&choice);
getchar();
switch(choice)
case 1:
printf("Enter a new string: ");
fgets(str1, sizeof(str1), stdin);
str1[strlen(str1)-1]= '\0';
len=0;
while(str1[len] != '\0')
len++;
printf("Length of the string: %d\n", len);
break;
case 2:
printf("Enter a string to reverse: ");
fgets(str1, sizeof(str1), stdin);
str1[strlen(str1)-1]= '\0';
len=0;
while (str1[len] !='\0' )
len ++;
start=0;
end=len-1;
while(start<end)
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start++;
end--;
printf("Reversed string: %s\n", str1);
break;
case 3:
printf ("Enter the source String: ");
fgets(str1,sizeof(str1),stdin);
str1[strlen(str1)-1]='\0';
printf("Enter the destination string: ");
str2[strlen(str2)-1]='\0';
i=0;
while(str1[i] !='\0')
str2[i]=str1[i];
i++;
str2[i]='\0';
printf("Source string: %s\n",str1);
printf("Copied string: %s\n",str2);
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)
printf("strings are equal. \n");
else
if (str1[i]=='\0'&&str2[i]!='\0')
printf("First string is smaller.\n");
else if(str1[i]!='\0'&&str2[i]=='\0')
printf("First string is greater.\n");
else
printf("strings are not equal.\n");
}
break;
case 5:
printf("Enter the first string: ");
fgets(str1, sizeof(str1),stdin);
str1[strlen(str1)-1]='\0';
printf("Enter the second string: ");
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';
printf("Concctenated string: %s\n",str1);
break;
case 6:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid Choice.Please Try Again.\n");
return 0;
Algorithm:
1. Start the program.
2. Loop continuously until the user chooses to exit:
1. Display the menu options:
1: Find string length
2: Reverse a string
3: Copy a string
4: Compare two strings
5: Concatenate two strings
6: Exit the program
2. Accept the user’s choice.
3. Perform the corresponding operation using a switch statement:
Case 1: Find String Length
1. Accept a string from the user.
2. Count characters until '\0'.
3. Display the length.
Case 2: Reverse a String
1. Accept a string from the user.
2. Find the length of the string.
3. Swap characters from start and end until the middle is reached.
4. Display the reversed string.
Case 3: Copy a String
1. Accept a source string from the user.
2. Copy each character from the source string to the destination string.
3. Display both strings.
Case 4: Compare Two Strings
1. Accept two strings from the user.
2. Compare each character one by one.
3. If they are equal, print "Strings are equal".
4. Otherwise, determine which one is greater or smaller and display the result.
Case 5: Concatenate Two Strings
1. Accept two strings from the user.
2. Append the second string to the first.
3. Display the concatenated string.
Case 6: Exit the Program
1. Print an exit message.
2. Terminate the program.
4. If the user enters an invalid choice, display an error message.
3. End the program.
Hand Run (Step By Step) :
Case 1: Find Length of String
Step Code Execution len Value Condition Check
1 choice = 1 - -
2 str1 = "JOHN" - -
3 len = 0 len = 0 -
4 while (str1[len] != '\0') "J" != '\0' → True Continue
5 len++ len = 1 -
6 while (str1[len] != '\0') "O" != '\0' → True Continue
7 len++ len = 2 -
8 while (str1[len] != '\0') "H" != '\0' → True Continue
Step Code Execution len Value Condition Check
9 len++ len = 3 -
10 while (str1[len] != '\0') "N" != '\0' → True Continue
11 len++ len = 4 -
12 while (str1[len] != '\0') '\0' == '\0' → False Exit loop
13 printf("Length: 4") Output: "Length of the string: 4"
Case 2: Reverse String
Step Code Execution start end Condition Check str1
1 choice = 2 - - - -
2 str1 = "GOOD" - - - "GOOD"
3 len = 4 - - - "GOOD"
4 start = 0, end = 3 start = 0 end = 3 start < end → True "GOOD"
5 swap G ↔ D start = 1 end = 2 - "DOOG"
6 start < end → True start = 1 end = 2 Continue "DOOG"
7 swap O ↔ O start = 2 end = 1 - "DOOG"
8 start < end → False Exit loop - -
9 printf("DOOG") Output: "Reversed string: DOOG"
Case 3: Copy String
Step Code Execution i Value Condition Check str2
1 choice = 3 - - -
2 str1 = "HELLO" - - "HELLO"
3 i=0 i=0 - -
4 while (str1[i] != '\0') "H" != '\0' → True Continue
5 str2[i] = str1[i] i=1 - "H"
Step Code Execution i Value Condition Check str2
6 while (str1[i] != '\0') "E" != '\0' → True Continue
7 str2[i] = str1[i] i=2 - "HE"
8 while (str1[i] != '\0') "L" != '\0' → True Continue
9 str2[i] = str1[i] i=3 - "HEL"
10 while (str1[i] != '\0') "L" != '\0' → True Continue
11 str2[i] = str1[i] i=4 - "HELL"
12 while (str1[i] != '\0') "O" != '\0' → True Continue
13 str2[i] = str1[i] i=5 - "HELLO"
14 while (str1[i] != '\0') '\0' == '\0' → False Exit loop
15 printf("Copied string: HELLO") Output: "Copied string: HELLO"
Case 4: Compare Strings (ASHRAF, AHMED)
Step Code Execution i Value Comparison Result
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
6 "ASHRAF" > "AHMED" First string is greater
printf("First string is Output: "First string is
7
greater.") greater."
Case 5: Concatenate Strings (EXCELLENT, WORK)
Step Code Execution i j Condition Check str1
1 choice = 5 - - - -
Step Code Execution i j Condition Check str1
2 str1 = "EXCELLENT" - - - "EXCELLENT"
3 str2 = "WORK" - - - "WORK"
4 Find end of str1 i=9 - - "EXCELLENT"
5 while (str2[j] != '\0') "W" != '\0' → True
6 Append 'W' i = 10 j=1 "EXCELLENTW"
7 Append 'O' i = 11 j=2 "EXCELLENTWO"
8 Append 'R' i = 12 j=3 "EXCELLENTWOR"
9 Append 'K' i = 13 j=4 "EXCELLENTWORK"
printf("Concatenated
Output:
10 string:
"EXCELLENTWORK"
EXCELLENTWORK")
Case 6: Exit
Step Code Execution Value
1 choice = 6 6
2 printf("Exiting the program.") Output: "Exiting the program."
3 return 0 End of program
OUTPUT:
Flow Chart: