C Programming - Strings

Exercise : Strings - Find Output of Program
26.
What will be the output of the program ?
#include<stdio.h>
void swap(char *, char *);

int main()
{
    char *pstr[2] = {"Hello", "IndiaBIX"};
    swap(pstr[0], pstr[1]);
    printf("%s\n%s", pstr[0], pstr[1]);
    return 0;
}
void swap(char *t1, char *t2)
{
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}
IndiaBIX
Hello
Address of "Hello" and "IndiaBIX"
Hello
IndiaBIX
Iello
HndiaBIX
Answer: Option
Explanation:

Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.

Step 2: char *pstr[2] = {"Hello", "IndiaBIX"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to

pstr[0] = "Hello", pstr[1] = "IndiaBIX"

Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.

If the swap function is "called by reference" it will affect the variable pstr.

Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].

Hence the output of the program is

Hello
IndiaBIX


27.
What will be the output of the program (Turbo C in 16 bit platform DOS) ?
#include<stdio.h>
#include<string.h>

int main()
{
    char *str1 = "India";
    char *str2 = "BIX";
    char *str3;
    str3 = strcat(str1, str2);
    printf("%s %s\n", str3, str1);
    return 0;
}
IndiaBIX India
IndiaBIX IndiaBIX
India India
Error
Answer: Option
Explanation:

It prints 'IndiaBIX IndiaBIX' in TurboC (in 16 bit platform).

It may cause a 'segmentation fault error' in GCC (32 bit platform).


28.
If the size of pointer is 4 bytes then What will be the output of the program ?
#include<stdio.h>

int main()
{
    char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
    printf("%d, %d", sizeof(str), strlen(str[0]));
    return 0;
}
22, 4
25, 5
24, 5
20, 2
Answer: Option
Explanation:

Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared as an pointer to the array of 6 strings.

Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));

sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'

strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';

Hence the output of the program is 24, 5

Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes).


29.
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;
}
The string is not empty
The string is empty
No output
0
Answer: Option
Explanation:

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".


30.
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;
}
Unpredictable integer value
0
-1
Error
Answer: Option
Explanation:

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.