Strings Practice

A fun thing to do in these exercises is to create a separate function and call it in main. That way, you can have your own library of functions and reuse it if you need.

  1. Write a function that receives a string and return its length without using any libraries.

  2. Write a function that receives a string and turns it into uppercase.

  3. Write a function that receives two strings and checks if they are equal.

  4. Write a function that receives a string and check if it is a palindrome.

  5. Write a function that receives a string and reverses it.

  6. CHALLENGE: How would you read a first and last name from the terminal, save it to a string, and display it to the screen?


More Practice Using Strings

Originally created by Idel Martinez and Jerrett Longworth in Fall 2020.

For all questions, assume all necessary libraries are included, and all function calls are properly defined. Assume that remove_clrf(string); will remove any newline characters (‘\n’ or ‘\r’) at the end of string passed in.

(Advanced: View the contents of remove_clrf().)
  void remove_crlf(char *s)
  {
    char *t = s + strlen(s) - 1;

    while ((t >= s) && (*t == '\n' || *t == '\r'))
    {
      *t = '\0';
      t--;
    }
  }

  1. In how many lines will the print statement in line 5 output?
void boring_print(void)
{
  char buffer[128] = "I am a string\nth\nat\n\t\tis\nsplit weirdly...\n :S\n";

  printf("%s", buffer);
}
  1. What will the following function output?
void print_bob_name(void)
{
  char buffer[128] = "My name is Bob\n";
  buffer[2] = '\0';

  printf("The string is %s\n", buffer);
}
  1. What does this function print?
void funky_print(void)
{
  char buffer[128] = "How are you?";

  for (int i = 0; buffer[i] != '\0'; i++) // What is this doing?
  {
    printf("%c", buffer[i]); // omg a %c !!
  }
}
  1. Similar to the previous question, what does function print?
void cool_print(void)
{
  char buffer[128] = "How are you?";
  int length = strlen(buffer);

  for (int i = 0; i < length; i++) // What is this doing?
  {
    printf("%c", buffer[i]);
  }
}
  1. In how many lines will the print statement in line 6 output?
int main(void)
{
  char buffer[128];
  printf("How is life like in Jupiter? ");
  fgets(buffer, 127, stdin);
  printf("The user wrote life is %s in Jupiter.\n", buffer);

  return 0;
}
  1. In how many lines will the print statement in line 9 output?
int main(void)
{
  char buffer[128];

  printf("How is life like in Jupiter? ");
  fgets(buffer, 127, stdin);
  remove_crlf(buffer);

  printf("The user wrote life is %s in Jupiter.\n", buffer);

  return 0;
}
  1. What is the output if the user entered “not good”?
int main(void)
{
  char buffer[128];

  printf("How is life like in Jupiter? ");
  fgets(buffer, 127, stdin);
  remove_crlf(buffer);
  remove_crlf(buffer);
  remove_crlf(buffer);

  printf("The user wrote life is %s in Jupiter.\n", buffer);

  return 0;
}
  1. What is the output if the user entered “3.14”?
void print_fav_num(void)
{
  char buffer[128];
  int fav_num;

  printf("What is your favorite number? ");
  fgets(buffer, 127, stdin);
  remove_crlf(buffer);
  fav_num = atoi(buffer);

  printf("Your number is %d!\n", fav_num);
}
  1. How many lines total will the following program print out?
void ignore_user_input(void)
{
  char buffer[128];
  printf("Hello.\nHow\nAre\nYou? ");
  fgets(buffer, 127, stdin);

  printf("Hello.\n");
  printf("How\n");
  printf("Are\n");
  printf("You? ");
  fgets(buffer, 127, stdin);

  printf("I don't really care, so goodbye!\n");
}

Challenge: Let’s look at some more complex functions!

  1. What will be the function output if the user runs the program two times? The first time, the user enters “yes” and “179.67”. The second time, the user enters “no”.
void multiply(void)
{
  char buffer[128] = "100.23134";
  double num;

  printf("The number is %s! But sadly, it is a string.\n", buffer);
  printf("Converting the string number to a double...\n");
  num = atof(buffer);

  printf("It should now be a number, let's do some math!\n");
  printf("%lf * 71 = %lf\n", num, num * 71);

  printf("Would you like to multiply %lf by some number? ", num);
  // Here I go overwriting the buffer, oh no! What is going to happen?!
  fgets(buffer, 127, stdin);
  remove_crlf(buffer);

  printf("You entered %s\n", buffer);
  if (strcmp(buffer, "yes") == 0) // User wants to multiply our number by something.
  {
    printf("Amazing! What do you want to multiply the number by? ");
    fgets(buffer, 127, stdin);
    remove_crlf(buffer);
    printf("Cool beans, you want to multiply it by %s. Let's do it.\n", buffer);

    printf("%lf * %s = %lf\n", num, buffer, num * atof(buffer));
  }
  else
  {
    printf(":( Fine.\n");
  }

  printf("I am exiting now...\n");
}
  1. What will the following functions print out if the user enters “2.718” for both print_num and print_another_num?
void print_num(void)
{
  char buffer[128];
  float a;

  printf("What is a? ");
  fgets(buffer, 127, stdin);
  remove_crlf(buffer);

  printf("You said that a is %s\n", buffer);
  printf("I am going to set a equal to that\n");

  a = buffer;

  printf("a is %f\n", a);
}

void print_another_num(void)
{
  char buffer[128];
  float a;

  printf("What is a? ");
  fgets(buffer, 127, stdin);
  remove_crlf(buffer);

  printf("You said that a is %s\n", buffer);
  printf("I am going to set a equal to that\n");

  a = atof(buffer);

  printf("a is %f\n", a);
}

  1. Challenge Program:

Create a program that asks the user for grades, prompting them to keep entering grades and saving them into an array. Once the user is done or the program reaches the maximum amount of grades that can be entered, print out the list of grades in the format below, along with the average.

As a requirement, make sure to include the following three function signatures:

You can create additional helper functions if you need them.

Example. If the user enters 7.3, 8.4, and 9.0. Your program will print the following:

You entered 3 grades. Here is a list of them:

7.30, 8.40, 9.00

Based on the previous grades, the average is: 8.23

As an extra challenge, find the maximum and minimum grades the user entered and print them.