Reading of strings

Program 13) Reading of strings using %wc and %ws.

In program illustrates the use of various field specifications for reading strings. When we use %wc for reading a string. the system will wait until the wth character is keyed in. Note that the specification %s terminates reading at the encounter of a blank space.
Therefore, name2 has read only the first part of "NEW YORK" and the second part is automatically assigned to name3. However, during the second run, the string "New-York" is correctly assigned to name2.

                                 PROGRAM

main( )
 {
      int no ;
      char name1[15], name2[15], name3[15] ;
      
      printf("Enter serial number and name one \n") ;
      scanf("%d %15c", &no, name) ;
      printf("%d %15s\n\n", no, name1) ;
      printf("Enter serial number and name two\n");
      scanf("%d %s", &no, name2) ;
      printf("%d %15s", &no, name2) ;
    
      printf("Enter serial number and name three\n") ;
      scanf("%d %15s", &no, name3) ;
      printf("%d %15s\n\n", no, name3) ;
      getch( ) ;
 }

Output:           Enter serial number and name one
                       1  123456789012345
                       1 123456789012345r
                       Enter serial number and name two
                       2 New York
                       2                    New
                       Enter serial number and name three
                       2                    York
                       Enter serial number and name one
                       1 123456789012
                       1 123456789012r
                       Enter serial number and name two
                       2 New York
                       2                   New-York
                       Enter serial number and name three
                       3 London
                       3                   London


No comments:

Post a Comment