Tuesday, March 1, 2011

Character Arrays and Strings

Write a program to read a series of words from a terminal using scanf functoin.
The program shown reads four  words and displays them on the screen. Note that the string 'Oxford Road' is treated as two words while the string 'Oxford-Road' as one word.

#include <stdio.h>
void main()
{
char word1[40],word2[40],word3[40],word4[40];
printf("Enter text: \n");
scanf("%s %s",word1,word2);
scanf("%s",word3);
scanf("%s",word4);
printf("\n");
printf("word1 = %s\nword2 = %s\n",word1,word2);
printf("word3 = %s\nword4 = %s\n",word3,word4);
getch();
}

OUTPUT
Enter text :
Oxford  Road,  London  M17ED
word1 = Oxford
word2 = Road,
word3 = London
word4 = M17ED

Enter text :
Oxford-Road, London-M17ED United Kingdom
word1 = Oxford-Road
word2 = London-M17ED
word3 = United
word4 = Kingdom

No comments:

Post a Comment