Wednesday, March 2, 2011

Character Arrays and Strings

Write a program to do the following:
(a) To output the question "Who is the inventor of C?"
(b) To accept an answer.
(c) To print out "Good" and then stop, if the answer is correct.
(d) To output the message 'try again', if the answer is wrong.
(e) To display the correct answer when the answer is wrong even at the third attempt and stop.

#include <stdio.h>
#include <string.h>
main()
{
char str[]="Dennis M Ritchie",str1[20];
int i,y;
clrscr();
printf("Who is the inventor of C?\n");
for(i=1;i<=3;i++)
{
gets(str1);
y=strcmp(str,str1);
if(y==0)
{
printf("Good");
break;
}
else
printf("Try again\n");
}
if(y!=0)
printf("Answer is Dennis M Ritchie");
getch();
}

OUTPUT
Who is the inventor of C?
Allen M turing
Try again
Thomas alva Addition
Try again
Charles Babbage
Try again
Answer is Dennis M Ritchie

Who is the inventor of C?
Dennis M Ritchie
Good

No comments:

Post a Comment