Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 19, 2017 22:19
Show Gist options
  • Save rszewczyk/b16aa26c4854c6f94a9755fa1c91371e to your computer and use it in GitHub Desktop.
Save rszewczyk/b16aa26c4854c6f94a9755fa1c91371e to your computer and use it in GitHub Desktop.
Class 14 - 10/19/2017

Assignment - Class #14 10/19/2017

In this assigment you will learn about strings in C.

As part of this assigment you will create a program in a file called strings.c. When you complete the assigment you will submit strings.c by uploading it to Slack. If you do not finish today, you have until the end of class (6:45pm) on Thursday, 10/26/2017 to turn it in. Note this doesn't necessarily mean you will have time to work on it in class on 10/26. If don't finish today, you should try and finish it before next Thursday.

Step 1

Copy and paste the code below into a new file and save it. Give it the name strings.c. Upload this file to the UMBC Linux server and compile it.

#include <stdio.h>
#include <string.h>

int main()
{
  char userName[50];
  strcpy(userName, "Rob");

  printf("Hello, %s\n", userName);

  return 0;
}

Run the program and observe the output. If everything worked, you should see something like this:

➜  gcc strings.c
➜  ./a.out
Hello, Rob
➜

Look closely at line 6. Aside from the [50] you should recognize this as very similar to the declaration of a variable of type char. What this line does is define a variable that holds a sequence of characters. The [50] notation is what sets it apart from a normal char variable that holds one character. The [] changes it to the sequence type and the 50 describes how many characters the sequence can hold. In programming, a sequence of characters is called a string. So, char userName[50] means declare a variable that can hold a string of length 50 or less.

In the same way that numeric variables can hold literal values (e.g. int can hold the value 4), strings also have their own corresponding representation of a literal value. In fact you've been using them all along. Sequences of characters enclosed in quotes are string values. Line 7 is using a function called strcpy (from string.h) to copy the contents of the value into the variable userName.

Also, note that output via the printf function works the same for strings as it does other variable types. In this case we have a new placeholder %s that we use with strings.

Step 2.

Replace the contents of your program with:

#include <stdio.h>

int main()
{
  char userName[50];

  printf("Enter your name: ");
  scanf("%s", userName);
  printf("Hello, %s\n", userName);

  return 0;
}

Transfer the program, recompile it and run it again. When prompted type your first name and press return. Observe the results.

Again, like other variables we can assign a string the result of user input with the scanf function. We use the same placeholder as with printf. Note that there is one small but very important difference when using scanf with strings vs the other data types we've learned so far. When using scanf with a string, do not use the & operator in front of the variable name.

Run the program again and this time type your first and last name, separated by a space. Observe the output. Here we can see an unfortunate limitation to scanf it only reads input until it encounters the first whitespace character - it could be the newline when you press return, or it could be a single space from the spacebar.

Step 3

Modify the program so that it prompts for the user's first name, then the user's last name and then prints a greeting using the user's full name. When you run it, it should look something like:

➜  gcc strings.c
➜  ./a.out
Enter your first name: Rob
Enter your last name: Szewczyk
Hello, Rob Szewczyk
➜

Objectives for Class 14

This material is not on the midterm. Class 13 is the cutoff for material covered on the midterm.

At the end of the class you will be able to:

  • Define string
  • Declare and use string variables
  • Use the scanf and printf functions with string variables and values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment