Raksha is developing a program to compare the lengths and content of two strings. She wants to write a program that takes two strings without any spaces as input and determines if they have the same length using the strlen function and content using the strcmp function. The program should print the length of each string and whether the strings are the same or not.Input format :The first line of input consists of a string, str1 representing the first string.The second line of input consists of a string, str2 representing the second string.Output format :The first line displays "Length of Str1 is " followed by an integer representing the length of the first string.The second line displays "Length of Str2 is " followed by an integer representing the length of the second string.The third line of output displays "The strings are the same" if they have the same content. Otherwise, it displays "The strings are not the same".
Question
Raksha is developing a program to compare the lengths and content of two strings. She wants to write a program that takes two strings without any spaces as input and determines if they have the same length using the strlen function and content using the strcmp function. The program should print the length of each string and whether the strings are the same or not.Input format :The first line of input consists of a string, str1 representing the first string.The second line of input consists of a string, str2 representing the second string.Output format :The first line displays "Length of Str1 is " followed by an integer representing the length of the first string.The second line displays "Length of Str2 is " followed by an integer representing the length of the second string.The third line of output displays "The strings are the same" if they have the same content. Otherwise, it displays "The strings are not the same".
Solution
Here is a simple C program that accomplishes what Raksha wants:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
printf("Length of Str1 is %d\n", strlen(str1));
printf("Length of Str2 is %d\n", strlen(str2));
if(strcmp(str1, str2) == 0)
printf("The strings are the same\n");
else
printf("The strings are not the same\n");
return 0;
}
This program first includes the necessary header files. It then defines the main function where two character arrays str1 and str2 are declared to hold the input strings. The program then prompts the user to enter the first and second strings. The strlen function is used to print the length of each string. The strcmp function is used to compare the two strings. If strcmp returns 0, it means the strings are the same and the program prints "The strings are the same". Otherwise, it prints "The strings are not the same".
Similar Questions
What does the strcmp() function return when both the strings are equal?
Write a C program using string functions to perform the following functionalities: Assume: The array size as 50.To concatenate two strings.To find the length of a string.To copy one string to another.To find the size of a string.To compare two strings.Write each functionality as different function and call it from the main program.Sample InputEnter String1: VelloreEnter String2: Chennai
Which of the following functions can be used to find the length of a string in C?a.length()b.strlength()c.strlen()d.size()
Which of the library function put back the length of a string?a.strlenb.strcatc.strcmpd.strcpy
Which of the library function is managed to compare two strings ?a.strlenb.strcatc.strcmpd.strcpy
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.