C LAB‎ > ‎(Sem1) Introduction to C‎ > ‎

WAP to enter two strings and get them to display.

posted Oct 30, 2010, 2:23 AM by Neil Mathew   [ updated Nov 6, 2010, 8:35 PM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
 
int main()
{
 
char S1[20];
char S2[20]; //the datatype string doesn't exist in C.
 
//method 1: can only take one word at a time (till spaces).
printf("\n Enter the first string: ");
scanf(" %s ", S1);
 
//method 2: can take whole line including spaces.
printf(" Enter the second string: ");
gets(S2); 
 
printf("\n The Strings are:\n 1st: %s\n 2nd: ",S1);
puts(S2);
 
return 0;
 
}
 

OUTPUT:

 Enter the first string: Hello
 Enter the second string: World
 The Strings are:
 1st: Hello
 2nd: World
 Enter the first string: New York
 Enter the second string: 
 The Strings are:
 1st: New
 2nd: York
 Enter the first string: New York er
 Enter the second string: 
 The Strings are:
 1st: New
 2nd: York er
Comments