Previous: 4.7 Summary
Up: 4 Processing Character Data
Next: 4.9 Problems
Previous Page: 4.7 Summary
Next Page: 4.9 Problems

4.8 Exercises

  1. What is the value of each of the following expressions:
    ch = 'd';
    

    (a) ((ch >= 'a') && (ch <= 'z')) (b) ((ch > 'A') && (ch < 'Z')) (c) ((ch >= 'A') && (ch <= 'Z')) (d) ch = ch -'a' + 'A'; (e) ch = ch - 'A' + 'a';

  2. What will be the output of the following:
    char ch;
         int d;
    

    ch = 'd'; d = 65; printf("ch = %c, value = %d\n", ch, ch); printf("d = %d, d = %c\n", d, d);

  3. Write the header file category.h discussed in section 4.1.2. Write the macros IS_UPPER(), IS_DIGIT(), IS_PUNCT(), IS_SPACE(), IS_CONTROL().
  4. Write a code fragment to test:
  5. Write separate loops to print out the ASCII characters and their values in the ranges:
    'a' to 'z',
         'A' to 'Z',
         '0' to '9'.
  6. Are these the same: 'a' and "a"? What is the difference between them?

  7. What will be the output of the source code:
    #define SQ(x) ((x) * (x))
         #define CUBE(x)  ((x) * (x) * (x))
         #define DIGITP(c) ((c) >= '0' && (c) <= '9')
    

    char c = '3';

    if (DIGITP(c)) printf("%d\n", CUBE(c - '0')); else printf("%d\n", SQ(c - '0'));

  8. Find the errors in the following code that was written to read characters until end of file.
    char c;
    

    while (c = getchar()) putchar(c);

  9. What will be the output of the following program?
    #include <stdio.h>
    main()
    {    int n, sum;
         char ch;
    

    ch = 'Y'; sum = 0; scanf("%d", &n); while (ch != 'N') { sum = sum + n; printf("More numbers? (Y/N) "); scanf("%c", &ch); scanf("%d", &n); } }

  10. What happens if scanf() is in a loop to read integers and a letter is typed?

  11. What happens if scanf() reads an integer and then attempts to read a character?

  12. Use a switch statement to test if a digit symbol is an even digit symbol.

  13. Write a single loop that reads and prints all integers as long as they are between 1 and 100 with the following restrictions: If an input integer is divisible by 7 terminate the loop with a break statement; if an input integer is divisible by 6, do not print it but continue the loop with a continue statement.



Previous: 4.7 Summary
Up: 4 Processing Character Data
Next: 4.9 Problems
Previous Page: 4.7 Summary
Next Page: 4.9 Problems

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:29:11 HST 1994