C Programming - Strings

Exercise : Strings - Point Out Correct Statements
1.
Which of the following statements are correct about the program below?
#include<stdio.h>

int main()
{
    char str[20], *s;
    printf("Enter a string\n");
    scanf("%s", str);
    s=str;
    while(*s != '\0')
    {
        if(*s >= 97 && *s <= 122)
            *s = *s-32;
        s++;
    }
    printf("%s",str);
    return 0;
}
The code converts a string in to an integer
The code converts lower case character to upper case
The code converts upper case character to lower case
Error in code
Answer: Option
Explanation:

This program converts the given string to upper case string.

Output:

Enter a string: indiabix

INDIABIX


2.
Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";
1: There is no difference in the declarations and both serve the same purpose.
2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer.
3: The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed.
4: In both cases the '\0' will be added at the end of the string "Sanjay".
1, 2
2, 3, 4
3, 4
2, 3
Answer: Option
Explanation:
No answer description is available. Let's discuss.

3.
Which of the following statements are correct ?
1: A string is a collection of characters terminated by '\0'.
2: The format specifier %s is used to print a string.
3: The length of the string can be obtained by strlen().
4: The pointer CANNOT work on string.
1, 2
1, 2, 3
2, 4
3, 4
Answer: Option
Explanation:

Clearly, we know first three statements are correct, but fourth statement is wrong. because we can use pointer on strings. Eg. char *p = "IndiaBIX".


4.
Which of the following statement is correct?
strcmp(s1, s2) returns a number less than 0 if s1>s2
strcmp(s1, s2) returns a number greater than 0 if s1<s2
strcmp(s1, s2) returns 0 if s1==s2
strcmp(s1, s2) returns 1 if s1==s2
Answer: Option
Explanation:

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

if s1 > s2 returns a value > 0

From the above statements, that the third statement is only correct.