Tuesday, March 1, 2011

Character Arrays and Strings

Write a program to read a line of text containing a series of words from the terminal.

#include <stdio.h>
void main( )
   {
       char  line[81], character;
       int   c;
       c = 0;
       printf("Enter text. Press at end\n");
       do
       {
    character = getchar();
    line[c]   = character;
    c++;
       }
       while(character != '\n');
       c = c - 1;
       line[c] = '\0';
       printf("%s\n", line);
       getch();
   }

OUTPUT
Enter text. Press at end
Programming in C is interesting.
Programming in C is interesting.

Enter text. Press at end
National Centre for Expert System, Hyderabad.
National Centre for Expert System, Hyderabad.

No comments:

Post a Comment