Which of the following statements would produce a True result given this code?char s1[] = "alpha";char s2[] = "alpha";char s3[] = "beta";char s4[] = "gamma";Select one:a.strcmp(s1, s2);b.strcmp(s1, s3);c.strcmp(s3, s4);d.strcmp(s1, "Alpha");
Question
Which of the following statements would produce a True result given this code?char s1[] = "alpha";char s2[] = "alpha";char s3[] = "beta";char s4[] = "gamma";Select one:a.strcmp(s1, s2);b.strcmp(s1, s3);c.strcmp(s3, s4);d.strcmp(s1, "Alpha");
Solution
The correct answer is a. strcmp(s1, s2);
Explanation:
The strcmp() function in C compares two strings character by character. If the first character of two strings is equal, the next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached.
a. strcmp(s1, s2); - This will return 0 because the strings "alpha" and "alpha" are identical. In the context of a boolean expression, 0 is considered as False in C. So, this option is incorrect.
b. strcmp(s1, s3); - This will return a negative value because the string "alpha" is less than the string "beta". In the context of a boolean expression, any non-zero value is considered as True in C. So, this option is correct.
c. strcmp(s3, s4); - This will return a negative value because the string "beta" is less than the string "gamma". In the context of a boolean expression, any non-zero value is considered as True in C. So, this option is correct.
d. strcmp(s1, "Alpha"); - This will return a positive value because the string "alpha" is greater than the string "Alpha". In the context of a boolean expression, any non-zero value is considered as True in C. So, this option is correct.
So, the correct answer is b. strcmp(s1, s3);, c. strcmp(s3, s4);, and d. strcmp(s1, "Alpha");.
Similar Questions
Which one of the following is a valid statement? char[] c = new char();char[] c = new char[5]; char[] c = new char(4); char[] c = new char[];
Which of the following statements are valid?a. int i = new int(30);b. double d[] = new double[30];c. char[] r = new char(1..30);d. int i[] = (3, 4, 3, 2);e. float f[] = {2.3, 4.5, 6.6};f. char[] c = new char()
Which one of the following statements is correct?Select one:strcmp(s1, s2) returns 0 if s1==s2strcmp(s1, s2) returns 1 if s1==s2strcmp(s1, s2) returns a number greater than 0 if s1<s2strcmp(s1, s2) returns a number less than 0 if s1>s2
Consider these two statements: char v[6]; strcpy(v, "string");The best description of the situation after these two statements is that:Group of answer choicesThe call to strcpy() will corrupt memory and may result in a program run-time error at some later point in the program's executionA run-time array overflow will be detected, and the program will be halted and an error message printedThe call to strcpy() will be truncated, and only "tring" will be copied into vThe call to strcpy() is completely fine, and v will have the desired value of "string"The call to strcpy() will be truncated, and only "strin" will be copied into v
Select the correct answerWhat will the output of below given code snippet?#include <stdio.h>int Function(char *S){ if (S == NULL || *S ==' ') return 0; int C = 1, i = 1; while (*(S+C)) { if (S[C] != S[C-1]) { S[i] = S[C]; i++; } C++; } *(S+i)=' '; return i;} int main(){ char S[] = "codettantra"; Function(S); puts(S); return 0;}
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.