Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program ?

#include <stdio.h>
#include <string.h>

int main()
{
    char sentence[80];
    int i;
    printf("Enter a line of text\n");
    gets(sentence);
    for(i=strlen(sentence)-1; i >=0; i--)
        putchar(sentence[i]);
    return 0;
}

 

  • A.The sentence will get printed in same order as it entered
  • B.The sentence will get printed in reverse order
  • C.Half of the sentence will get printed
  • D.None of above

Answer: B

Correct answer: The sentence will get printed in reverse order

The loop starts at strlen(sentence)-1, the last character, and counts down to 0, writing each character as it goes - so the line comes out backwards. (Note that gets() cannot limit the input length and has been removed from modern C; fgets() is what real code should use.)

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.