Exercise: Strings

Questions for: Strings

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

int main()
{
    static char s[25] = "The cocaine man";
    int i=0;
    char ch;
    ch = s[++i];
    printf("%c", ch);
    ch = s[i++];
    printf("%c", ch);
    ch = i++[s];
    printf("%c", ch);
    ch = ++i[s];
    printf("%c", ch);
    return 0;
}
A:
hhe!
B:
he c
C:
The c
D:
Hhec
Answer: A
No answer description is available. Let's discuss.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    static char s[] = "Hello!";
    printf("%d\n", *(s+strlen(s)));
    return 0;
}
A:
8
B:
0
C:
16
D:
Error
Answer: B
No answer description is available. Let's discuss.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    static char str1[] = "dills";
    static char str2[20];
    static char str3[] = "Daffo";
    int i;
    i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
    printf("%d\n", i);
    return 0;
}
A:
0
B:
1
C:
2
D:
4
Answer: A
No answer description is available. Let's discuss.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

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

The function strlen returns the number of characters int the given string.

Therefore, strlen(str) becomes strlen("India") contains 5 characters. A string is a collection of characters terminated by '\0'.

The output of the program is "5".

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

int main()
{
    char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
    int i;
    char *t;
    t = names[3];
    names[3] = names[4];
    names[4] = t;
    for(i=0; i<=4; i++)
        printf("%s,", names[i]);
    return 0;
}
A:
Suresh, Siva, Sona, Baiju, Ritu
B:
Suresh, Siva, Sona, Ritu, Baiju
C:
Suresh, Siva, Baiju, Sona, Ritu
D:
Suresh, Siva, Ritu, Sona, Baiju
Answer: B

Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is declared as an pointer to a array of strings.

Step 2: int i; The variable i is declared as an integer type.

Step 3: char *t; The variable t is declared as pointer to a string.

Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.

Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names.

Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".

Ad Slot (Above Pagination)
Quiz