CS156: Intro to C, Part I

Spring 2018

Strings

See this page as a slide show

CS156 Strings

Characters in C

printf("%d %d %d %d\n", 'C', '%', '2', '\n');
67 37 50 10

Strings in C

    ┌────┬────┬────┬────┬────┬────┐
    │ T  │ e  │ x  │ a  │ s  │ \0 │
    └────┴────┴────┴────┴────┴────┘
      0    1    2    3    4    5

Don’t forget the null character

    char name[7];
    name[0] = 'F'; name[1] = 'o'; name[2] = 'o';
    printf("%s\n", name);

    ┌───┬───┬───┬───┬───┬───┬───┐
    │ F │ o │ o │ ¥ │ ¶ │ € │ ☃ │
    └───┴───┴───┴───┴───┴───┴───┘
      0   1   2   3   4   5   6

Assigning Strings to Variables

Assigning Strings to Variables

Multiple ways to define a character array as a string:

// These all do the same thing:
char z1[5] = "Zulu";
char z2[] = "Zulu";
char z3[5] = { 'Z', 'u', 'l', 'u', '\0' };
char z4[] = { 'Z', 'u', 'l', 'u', '\0' };
/* This one has extra space for growth */
char z5[10] = { 'Z', 'u', 'l', 'u', '\0' };
printf("%s%s%s%s%s\n", z1, z2, z3, z4, z5);
ZuluZuluZuluZuluZulu

           ┌────┬────┬────┬────┬────┐
    z1…z4: │ Z  │ u  │ l  │ u  │ \0 │
           └────┴────┴────┴────┴────┘
             0    1    2    3    4

        ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
    z5: │ Z  │ u  │ l  │ u  │ \0 │ ?? │ ?? │ ?? │ ?? │ ?? │
        └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
          0    1    2    3    4    5    6    7    8    9    

The C String Library

strlen and the length of strings

A String Mystery

char alpha[] = "123";
char beta[16] = "1234567890abcdef";
char gamma[] = "12345678";
printf("%zu %zu %zu\n", sizeof(alpha), sizeof(beta), sizeof(gamma));
printf("%zu %zu %zu\n", strlen(alpha), strlen(beta), strlen(gamma));
4 16 9
3 19 8

strcat: Concatenating Strings

    char name[12];
    strcpy(name, "Sue");       // name = "Sue", length=3
    strcat(name, " Storm");    // name = "Sue Storm", length=9
    strcat(name, " Richards"); // Runtime failure: too long
    strcat("Ben", "Grimm");    // Compile-time failure
    char buf[10];
    strcat(buf, "xyz");        // Adding on to the end of … what?

strcmp: Comparing Strings

strcmp returns 0 if two strings are equal and otherwise returns a non-zero answer

char s1[] = "Russell Brand", s2[] = "Russell Brand";
if (strcmp(s1, s2) == 0)
    printf("The strings are equal.\n");
else
    printf("The strings are not equal.\n");
The strings are equal.

A common misunderstanding

It’s strcmp, not strequal. The result of 0 for equal strings can be confusing:

char s1[] = "Russell Brand", s2[] = "Russell Brand";
if (strcmp(s1, s2))  // Bad code!  Bad!
    printf("They're the same.\n");   // Liar!
else
    printf("The strings are not equal.\n");
The strings are not equal.

strcmp: Comparing Strings

int strcmp(const char s1[], const char s2[]);

Return value indicates the lexicographical relation of string1 to string2:

Relationship of s1 to s2Value
s1 less than s2some number <0
s1 identical to s2 0
s1 greater than s2some number >0

Vague, isn’t it?

char a[] = "omega", b[] = "alpha";
printf("%d\n", strcmp(a,b));
14

String Use Tips

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-18T12:59

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building