CS156

CS156: Intro to C, Part I

Fall 2017

More Strings

See this page as a slide show

CS156 MoreStrings: More Strings

puts

puts("hello");
printf("hello\n");
hello
hello

or:

char name[] = "Stephen Fry";
puts(name);
printf("%s\n", name);
Stephen Fry
Stephen Fry

Reading in a value

Could use scanf("%s", string); but…

char message[80];
printf("Enter a string:\n");
scanf("%s", message);
printf("The string just entered is:\n");
puts(message);
printf("The size is %d:\n", strlen(message));

Why is there no & for scanf?

Reading in a value

Could use scanf("%s", string); but…

char message[80];
printf("Enter a string: ");
scanf("%s", message);
printf("You said \"%s\"\n", message);
printf("Size: %d\n", strlen(message));
Enter a string: Jack Applin
You said "Jack"
Size: 4

That wasn’t what I wanted!

Reading Strings from a User

    fgets(name, length, stdin);
  • name — the name of the character array
  • length — the maximum number of characters to read (plus one for the '\0')
  • stdin — indicates to read from standard input

Reading in a value using fgets

char message[20];
printf("Enter a string: ");
fgets(message, sizeof(message), stdin);
printf("The string just entered is:\n");
puts(message);
printf("The size is: %d\n", strlen(message));
% ./a.out
Enter a string: The end is near!
The string just entered is:
The end is near!

The size is 17

Where did that blank line come from?

Example

    #include <stdio.h>

    int main() {
        char first[100], last[100];
        printf("Enter your first name: ");
        fgets(first, sizeof(first), stdin);
        printf("Enter your last name: ");
        fgets(last, sizeof(last), stdin);
        printf("Your name is: %s %s\n", first, last);
        return 0;
    }

Example, continued

    % c11 example.c
    % ./a.out
    Enter your first name: Cookie
    Enter your last name: Monster
    Your name is: Cookie
     Monster

That looks awful. We need to remove the newline from the end of the strings.

Example Fix

    // Chop the last character '\n' off the strings
    firstName[strlen(firstName)-1] = '\0';
    lastName[strlen(lastName)-1] = '\0'

Why the -1 ?

The null character isn’t magic

char s[] = "bonehead";
s[4] = '\0';
printf("%s\n", s);
bone
┌────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ b  │ o  │ n  │ e  │ h  │ e  │ a  │ d  │ \0 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┘
┌────┬────┬────┬────┬────┬────┬────┬────┬────┐
│ b  │ o  │ n  │ e  │ \0 │ e  │ a  │ d  │ \0 │
└────┴────┴────┴────┴────┴────┴────┴────┴────┘

Reading Other Input with fgets

sscanf example

sscanf(string, format, variables)

#include <stdio.h>

int main() {
    char line[400];
    float height, weight;
    // read two numbers from a user on one line
    printf("Enter your height and weight: ");
    fgets(line, sizeof(line), stdin);
    // read the two numbers from the string to two floats
    sscanf(line, "%f %f", &height, &weight);
    return 0;
}

Functions in string.h

#include <string.h>

NameDescription
strcat(s1, s2)Concatenates s2 onto s1
strcpy(s1, s2)Copies s2 to s1
strlen(s)Returns the length of s
strchr(s, c)Finds first occurrence of c in s
strcmp(s1, s2)Compares s1 to s2

Functions in ctype.h

#include <ctype.h>

NameDescription
isalpha(c)Is this a letter?
isupper(c)Is this an uppercase letter?
islower(c)Is this a lowercase letter?
isdigit(c)Is this a digit?
toupper(c)Convert character to uppercase
tolower(c)Convert character to lowercase

They return 0 for false, and something else for true. They work only on a single char—not a string!

Functions in stdlib.h

#include <stdlib.h>

NameDescription
atoi(s)Convert string to int
atof(s)Convert string to double

atoi = ASCII to integer
atof = ASCII to floating-point value

Understanding the fundamentals

void mystrcpy(char to[], char from[]) {
    for (int i=0; i<=strlen(from); i++)
        to[i] = from[i];
}

Understanding the fundamentals

Let’s avoid calling strlen so many times:

void mystrcpy(char to[], char from[]) {
    int len = strlen(from);
    for (int i=0; i<=len i++)
        to[i] = from[i];
}

Understanding the fundamentals

It’s faster to avoid calling strlen, if we care:

void mystrcpy(char to[], char from[]) {
    int i=0;
    while (from[i] != '\0') {
        to[i] = from[i]; 
        i++;
    }
    to[i] = '\0';
}

Strings as parameters to functions

    #define CONST_NAME value

Careful!

Modified: 2016-07-26T18:50

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building