C-Style String Worksheet Answers


  1. 
    void printstr(char string[])
    {
       int i;
       for (i = 0 ; string[i] != '\0' ; i++)
          cout << string[i];
    }
    


  2. No, because it prints all chars from the beginning of the string until it finds the null byte. The function assumes there is a null byte in the string because that is required for c-style strings.

  3. void readword(char string[], int maxch)
    {
        int i = 0;
        char ch;
        ch = cin.get();
        while (ch == '\n' || ch == '\t' || ch == ' ')
           ch = cin.get();
        while (ch != '\n' && ch != '\t' && ch != ' ' && i < maxch)
        {
           string[i] = ch;
           ch = cin.get();
           i++;
        }
        string[i] = '\0';
    }
    



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002