Lab 8 - Midterm 2 solutions and strings

The midterm 2 solution guide is posted in the Solutions section (opens a new window).

C-style strings are character arrays that end with the null character, '\0'. Ch 8.1 of the book covers C-style strings. In this lab, you are going to write a variation of the array get_input() function. The function will get a whole line of data from the keyboard, including any tabs and spaces the user types. This function will loop getting characters from the keyboard until the character '\n' is received, which means the user has hit enter and that is the end of the line, or until the maximum number of characters the string can hold has been reached. Use the following main:

int main()
{
  char line[121];  // Can hold 120 characters plus 1 for the null character

  // get_input takes the string and how many characters the string can hold
  get_input(line, 120);

  // Echo the line to the screen
  printf("You entered: %s\n", line);

  return 0;
}