Previous: 11.5 Summary
Up: 11 String Processing
Next: 11.7 Problems
Previous Page: 11.5 Summary
Next Page: 11.7 Problems

11.6 Exercises

  1. If the characters in an array, s are: string0 of characters0

    What does each of the following print? Show each character.

    printf("%s", s);
        puts(s);
        fputs(s, stdout);

  2. If the input of characters is:

    string of characters\n

    What does each of the following read? Show each character, including NULL.

    scanf("%s", s);
        gets(s);
        fgets(s, sizeof(s) - 1, stdin);

  3. Assume s is a string array. Under what condition is each of the following True?

    s
        *s
        !*s
        gets(s)
        *gets(s)
        !*gets(s)

    Find and correct any errors in the following and determine the outputs where feasible. The input is shown when appropriate.

  4. main()
    {   char s[80], t[80];
    

    s = "this is a message"; if (s == t) printf("Equal\n"); else printf("Not equal\n"); puts(s); }

  5. main()
    {   char s[80], t[80];
    

    scanf("%s", s); printf("%s", s); }

    Input: This is a message

  6. main()
    {   char *s;
    

    s = "this is a message"; printf("%d %s\n", s, s); puts(s); }

  7. main()
    {   char *s;
    

    gets(s); puts(s); }

  8. main()
    {   char s[80];
    

    while (*s) { putchar(*s); s++; } }

  9. main()
    {   char *s;
    

    strcpy(s, "hello"); puts(s); }

  10. main()
    {   char name[80];
    

    name = get_str(name); puts(s); }

    char *get_str(char *s) { gets(s); return s; }

  11. int cmpstr(char *s, char *t)
    {
        if (s == t)
             return TRUE;
        else
             return FALSE;
    }

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:04:57 HST 1994