Exercise: Strings

Questions for: Strings

What will be the output of the program ?
#include<stdio.h>

int main()
{
    char t;
    char *p1 = "India", *p2;
    p2=p1;
    p1 = "BIX";
    printf("%s %s\n", p1, p2);
    return 0;
}
A:
India BIX
B:
BIX India
C:
India India
D:
BIX BIX
Answer: B

Step 1: char *p1 = "India", *p2; The variable p1 and p2 is declared as an pointer to a character value and p1 is assigned with a value "India".

Step 2: p2=p1; The value of p1 is assigned to variable p2. So p2 contains "India".

Step 3: p1 = "BIX"; The p1 is assigned with a string "BIX"

Step 4: printf("%s %s\n", p1, p2); It prints the value of p1 and p2.

Hence the output of the program is "BIX India".

What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str1[] = "Hello";
    char str2[] = "Hello";
    if(str1 == str2)
        printf("Equal\n");
    else
        printf("Unequal\n");
    return 0;
}
A:
Equal
B:
Unequal
C:
Error
D:
None of above
Answer: B

Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and initialized with a string "Hello".

Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello".

We have use strcmp(s1,s2) function to compare strings.

Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.

Step 4: At the else part it prints "Unequal".

What will be the output of the program in Turbo C?
#include<stdio.h>

int main()
{
    char str[10] = "India";
    str[6] = "BIX";
    printf("%s\n", str);
    return 0;
}
A:
India BIX
B:
BIX
C:
India
D:
Error
Answer: D

str[6] = "BIX"; - Nonportable pointer conversion.

What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    char str1[5], str2[5];
    int i;
    gets(str1);
    gets(str2);
    i = strcmp(str1, str2);
    printf("%d\n", i);
    return 0;
}
A:
Unpredictable integer value
B:
0
C:
-1
D:
Error
Answer: A

gets() gets collects a string of characters terminated by a new line from the standard input stream stdin.

The gets(str1) read the input string from user and store in variable str1.

The gets(str2) read the input string from user and store in variable str2.

The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative or positive values. So the value of i is "unpredictable integer value".

printf("%d\n", i); It prints the value of variable i.

What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is not empty\n");
    else
        printf("The string is empty\n");
    return 0;
}
A:
The string is not empty
B:
The string is empty
C:
No output
D:
0
Answer: B

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.

In the else part it prints "The string is empty".

Ad Slot (Above Pagination)
Quiz