cs107 spring 2019, lecture 4 - stanford university...4 plan for today •characters •strings...
TRANSCRIPT
![Page 1: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/1.jpg)
1This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Marty Stepp, Cynthia Lee, Chris Gregg, and others.
CS107 Spring 2019, Lecture 4C Strings
Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3
![Page 2: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/2.jpg)
2
CS107 Topic 2: How can a computer represent and
manipulate more complex data like text?
![Page 3: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/3.jpg)
3
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans
![Page 4: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/4.jpg)
4
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans
![Page 5: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/5.jpg)
5
CharA char is a variable type that represents a single character or “glyph”.
char letterA = 'A';char plus = '+';char zero = '0';char space = ' ';char newLine = '\n';char tab = '\t';char singleQuote = '\'';char backSlash = '\\';
![Page 6: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/6.jpg)
6
ASCIIUnder the hood, C represents each char as an integer (its “ASCII value”).
• Uppercase letters are sequentially numbered• Lowercase letters are sequentially numbered• Digits are sequentially numbered• Lowercase letters are 32 more than their uppercase equivalents (bit flip!)
char uppercaseA = 'A'; // Actually 65char lowercaseA = 'a'; // Actually 97char zeroDigit = '0’; // Actually 48
![Page 7: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/7.jpg)
7
ASCIIWe can take advantage of C representing each char as an integer:
bool areEqual = 'A' == 'A'; // truebool earlierLetter = 'f' < 'c'; // falsechar uppercaseB = 'A' + 1;int diff = 'c' - 'a'; // 2int numLettersInAlphabet = 'z' – 'a' + 1;// orint numLettersInAlphabet = 'Z' – 'A' + 1;
![Page 8: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/8.jpg)
8
ASCIIWe can take advantage of C representing each char as an integer:
// prints out every lowercase characterfor (char ch = 'a'; ch <= 'z'; ch++) {
printf("%c", ch);}
![Page 9: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/9.jpg)
9
Common ctype.h FunctionsFunction Description
isalpha(ch) true if ch is 'a' through 'z' or 'A' through 'Z'
islower(ch) true if ch is 'a' through 'z'
isupper(ch) true if ch is 'A' through 'Z'
isspace(ch) true if ch is a space, tab, new line, etc.
isdigit(ch) true if ch is '0' through '9'
toupper(ch) returns uppercase equivalent of a letter
tolower(ch) returns lowercase equivalent of a letter
Remember: these return the new char, they cannot modify an existing char!
![Page 10: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/10.jpg)
10
Common ctype.h Functionsbool isLetter = isalpha('A'); // truebool capital = isupper('f'); // falsechar uppercaseB = toupper('b');bool digit = isdigit('4'); // true
![Page 11: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/11.jpg)
11
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans
![Page 12: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/12.jpg)
12
C StringsC has no dedicated variable type for strings. Instead, a string is represented as an array of characters.
char text[] = "Hello, world!";index 0 1 2 3 4 5 6 7 8 9 10 11 12
character 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!'
![Page 13: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/13.jpg)
13
Creating Stringschar myString[] = "Hello, world!"; // C figures out sizechar empty[] = "";
myString[0] = 'h';printf("%s", myString); // hello, world!
char stringToFillIn[30]; // or specify sizestringToFillIn[0] = 'a';...
![Page 14: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/14.jpg)
14
String LengthC strings are just arrays of characters. How do we determine string length?Option 1: reserve the first byte to store the length
index ? 0 1 2 3 4 5 6 7 8 9 10 11 12value 13 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!'
Pros Cons• Can get length in O(1) time! • Length is limited by size of 1
byte, and longer lengths need more bytes.
• Must compensate for indices (index 0 is length)
![Page 15: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/15.jpg)
15
String LengthC strings are just arrays of characters. How do we determine string length?Option 2: terminate every string with a '\0' character.
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13value 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
Pros Cons• Always uses exactly 1 extra
byte.• Doesn’t change indices of
other characters.
• Requires traversing the string to calculate its length.
![Page 16: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/16.jpg)
16
String LengthC strings use Option 2 – they are arrays of characters, ending with a null-terminating character '\0’.
Use the provided strlen function to calculate string length. The null-terminating character does not count towards the length.
char myStr[] = "Hello, world!";int length = strlen(myStr); // 13
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13value 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
Caution: strlen is O(N) because it must scan the entire string! Save the value if you plan to refer to the length later.
![Page 17: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/17.jpg)
17
C Strings As ParametersWhen you pass a string as a parameter, it is passed as a char *. C passes the location of the first character rather than a copy of the whole array.int doSomething(char *str) {
…}
char myString[] = "Hello";doSomething(myString);
![Page 18: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/18.jpg)
18
C Strings As ParametersWhen you pass a string as a parameter, it is passed as a char *. C passes the location of the first character rather than a copy of the whole array.int doSomething(char *str) {
…char secondChar = str[1]; // 'e'printf("%s\n", str); // prints Hello
}
char myString[] = "Hello";doSomething(myString);
You can still operate on the string the same way as with a char[].
![Page 19: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/19.jpg)
19
char *You can also create a char * variable yourself that points to an address within in an existing string.
char myString[] = "Hello";char *otherStr = myString; // points to 'H'
![Page 20: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/20.jpg)
20
char * vs. char[]char myString[]
vschar *myString
Both are essentially pointers to the first character in the string. However, you cannot reassign an array after you create it. You can reassign a pointer after you create it.
char myStringArr[] = "Hello, world!";myString = "Another string"; // not allowed!---char *myString = myStringArr;myString = myOtherString; // ok
![Page 21: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/21.jpg)
21
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans• Practice: Password Verification
![Page 22: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/22.jpg)
22
Common string.h FunctionsFunction Description
strlen(str) returns the # of chars in a C string (before null-terminating character).strcmp(str1, str2), strncmp(str1, str2, n)
compares two strings; returns 0 if identical, <0 if str1 comes before str2 in alphabet, >0 if str1 comes after str2 in alphabet. strncmpstops comparing after at most n characters.
strchr(str, ch)strrchr(str, ch)
character search: returns a pointer to the first occurrence of ch in str, or NULL if ch was not found in str. strrchr find the last occurrence.
strstr(haystack, needle) string search: returns a pointer to the start of the first occurrence of needle in haystack, or NULL if needle was not found in haystack.
strcpy(dst, src),strncpy(dst, src, n)
copies characters in src to dst, including null-terminating character. Assumes enough space in dst. Strings must not overlap. strncpystops after at most n chars, and does not add null-terminating char.
strcat(dst, src),strncat(dst, src, n)
concatenate src onto the end of dst. strncat stops concatenating after at most n characters. Always adds a null-terminating character.
strspn(str, accept),strcspn(str, reject)
strspn returns the length of the initial part of str which contains onlycharacters in accept. strcspn returns the length of the initial part of str which does not contain any characters in reject.
![Page 23: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/23.jpg)
23
Comparing StringsYou cannot compare C strings using comparison operators like ==, < or >. This compares addresses!
// e.g. str1 = 0x7f42, str2 = 0x654dvoid doSomething(char *str1, char *str2) {
if (str1 > str2) { … // compares 0x7f42 > 0x654d!Instead, use strcmp:
int compResult = strcmp(str1, str2);if (compResult == 0) {
// equal} else if (compResult < 0) {
// str1 comes before str2 } else {
// str1 comes after str2}
![Page 24: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/24.jpg)
24
Copying StringsYou cannot copy C strings using =. This copies character addresses!
char str1[] = "hello"; // e.g. 0x7f42char *str2 = str1; // 0x7f42. Points to same string!str2[0] = 'H';printf("%s", str1); // Helloprintf("%s", str2); // Hello
Instead, use strcpy:
char str1[] = "hello"; // e.g. 0x7f42char str2[6];strcpy(str2, str1);str2[0] = 'H';printf("%s", str1); // helloprintf("%s", str2); // Hello
![Page 25: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/25.jpg)
25
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'str1
0 1 2 3 4 5? ? ? ? ? ?str2
![Page 26: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/26.jpg)
26
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' ? ? ? ? ?str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 27: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/27.jpg)
27
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' 'e' ? ? ? ?str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 28: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/28.jpg)
28
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' 'e' 'l' ? ? ?str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 29: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/29.jpg)
29
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' ? ?str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 30: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/30.jpg)
30
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ?str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 31: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/31.jpg)
31
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 32: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/32.jpg)
32
Copying Stringschar str1[] = "hello";char str2[6];strcpy(str2, str1);
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'str2
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' '\0'
![Page 33: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/33.jpg)
33
Copying StringsYou are responsible for ensuring there is enough space in the destination to hold the entire copy, including the null-terminating character.
char str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
Writing past your memory bounds is called a “buffer overflow”. It can allow for security vulnerabilities!
![Page 34: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/34.jpg)
34
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5? ? ? ? ? ?str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
- other program memory -
![Page 35: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/35.jpg)
35
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' ? ? ? ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 36: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/36.jpg)
36
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' ? ? ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 37: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/37.jpg)
37
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' ? ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 38: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/38.jpg)
38
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 39: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/39.jpg)
39
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 40: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/40.jpg)
40
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 41: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/41.jpg)
41
- other program memory -' '
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 42: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/42.jpg)
42
- other program memory -' ' 'w'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 43: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/43.jpg)
43
- other program memory -' ' 'w' 'o'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 44: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/44.jpg)
44
- other program memory -' ' 'w' 'o' 'r'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 45: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/45.jpg)
45
- other program memory -' ' 'w' 'o' 'r' 'l'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 46: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/46.jpg)
46
- other program memory -' ' 'w' 'o' 'r' 'l' 'd'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 47: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/47.jpg)
47
- other program memory -' ' 'w' 'o' 'r' 'l' 'd' '!'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 48: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/48.jpg)
48
- other program memory -' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 49: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/49.jpg)
49
- other program memory -' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
Copying Stringschar str1[] = "hello, world!";char str2[6]; // not enough space!strcpy(str2, str1); // overwrites other memory!
str1
0 1 2 3 4 5'h' 'e' 'l' 'l' 'o' ','str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 50: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/50.jpg)
50
Copying Stringsstrncpy copies at most the first n bytes of src to dst. If there is no null-terminating character in these bytes, then dst will not be null terminated!
// copying "hello"char str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5); // doesn’t copy '\0'!
If there is no null-terminating character, we may not be able to tell where the end of the string is anymore. E.g. strlen may continue reading into some other memory in search of '\0'!
![Page 51: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/51.jpg)
51
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4? ? ? ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 52: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/52.jpg)
52
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4'h' ? ? ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 53: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/53.jpg)
53
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4'h' 'e' ? ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 54: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/54.jpg)
54
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4'h' 'e' 'l' ? ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 55: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/55.jpg)
55
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4'h' 'e' 'l' 'l' ?str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 56: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/56.jpg)
56
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4'h' 'e' 'l' 'l' 'o'str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 57: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/57.jpg)
57
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
0 1 2 3 4'h' 'e' 'l' 'l' 'o'str2 - other program memory -
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 58: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/58.jpg)
58
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 59: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/59.jpg)
59
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 60: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/60.jpg)
60
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 61: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/61.jpg)
61
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 62: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/62.jpg)
62
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 63: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/63.jpg)
63
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 64: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/64.jpg)
64
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 65: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/65.jpg)
65
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 66: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/66.jpg)
66
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 67: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/67.jpg)
67
Copying Stringschar str1[] = "hello, world!";char str2[5];strncpy(str2, str1, 5);int length = strlen(str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4'h' 'e' 'l' 'l' 'o' - other program memory -
![Page 68: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/68.jpg)
68
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13? ? ? ? ? ? ? ? ? ? ? ? ? ?
![Page 69: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/69.jpg)
69
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' ? ? ? ? ? ? ? ? ? ? ? ? ?
![Page 70: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/70.jpg)
70
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' ? ? ? ? ? ? ? ? ? ? ? ?
![Page 71: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/71.jpg)
71
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' ? ? ? ? ? ? ? ? ? ? ?
![Page 72: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/72.jpg)
72
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' ? ? ? ? ? ? ? ? ? ?
![Page 73: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/73.jpg)
73
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 74: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/74.jpg)
74
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 75: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/75.jpg)
75
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 76: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/76.jpg)
76
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 77: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/77.jpg)
77
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 78: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/78.jpg)
78
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 79: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/79.jpg)
79
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 80: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/80.jpg)
80
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 81: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/81.jpg)
81
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 82: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/82.jpg)
82
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 83: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/83.jpg)
83
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
![Page 84: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/84.jpg)
84
Copying Stringschar str1[14];char str2[] = "hello there";strncpy(str1, str2, 5);printf("%s\n", str1);
str1
0 1 2 3 4 5 6 7 8 9 10 11 12 13'h' 'e' 'l' 'l' 'o' ? ? ? ? ? ? ? ? ?
$ ./strncpy_buggy wonderfulword: wonderfulwordcopy: wonde⍰⍰J⍰⍰⍰
![Page 85: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/85.jpg)
85
Copying StringsIf necessary, make sure to add a null-terminating character yourself.
// copying "hello"char str1[] = "hello, world!";char str2[6]; // room for string and '\0'strncpy(str2, str1, 5); // doesn’t copy '\0'!str2[5] = '\0'; // add null-terminating char
![Page 86: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/86.jpg)
86
String Copying ExerciseWhat value should go in the blank at right?A. 4 (text code: 649421)B. 5 (text code: 649422)C. 6 (text code: 649427)D. 12 (text code: 649428)E. strlen(“hello”) (text code: 649429)F. Something else (text code: 649430)
char hello[] = "hello";char str[______];strcpy(str, hello);
Respond at pollev.com/nicktroccoli901 or text a code above to 22333.
![Page 87: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/87.jpg)
87This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Marty Stepp, Cynthia Lee, Chris Gregg, and others.
![Page 88: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/88.jpg)
88
Concatenating StringsYou cannot concatenate C strings using +. This adds character addresses!
char str1[] = "hello ";char str2[] = "world!";char *str3 = str1 + str2;// doesn’t compile!
Instead, use strcat:
char str1[13] = "hello ";// enough space for strings + '\0’char str2[] = "world!";strcat(str1, str2); // removes old '\0', adds new '\0' at endprintf("%s", str1); // hello world!
Both strcat and strncat remove the old '\0' and add a new one at the end.
![Page 89: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/89.jpg)
89
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' '\0' ? ? ? ? ? ?
![Page 90: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/90.jpg)
90
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' ? ? ? ? ? ?
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 91: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/91.jpg)
91
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' ? ? ? ? ?
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 92: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/92.jpg)
92
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' ? ? ? ?
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 93: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/93.jpg)
93
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' ? ? ?
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 94: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/94.jpg)
94
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' ? ?
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 95: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/95.jpg)
95
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '!' ?
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 96: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/96.jpg)
96
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 97: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/97.jpg)
97
Concatenating Stringschar str1[13] = "hello ";char str2[] = "world!";strcat(str1, str2);
str1
str2
0 1 2 3 4 5 6 7 8 9 10 11 12'h' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '!' '\0'
0 1 2 3 4 5 6'w' 'o' 'r' 'l' 'd' '!' '\0'
![Page 98: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/98.jpg)
98
SubstringsSince C strings are pointers to characters, we can adjust the pointer to omit characters at the beginning.
// Want just "car"char chars[] = "racecar";char *str1 = chars;
0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8'r' 'a' 'c' 'e' 'c' 'a' 'r' '\0'
str1
0xee0xf1
![Page 99: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/99.jpg)
99
SubstringsSince C strings are pointers to characters, we can adjust the pointer to omit characters at the beginning.
// Want just "car"char chars[] = "racecar";char *str1 = chars;char *str2 = chars + 4;
0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8'r' 'a' 'c' 'e' 'c' 'a' 'r' '\0'
str1
0xee0xf1 str2
0xd20xf5
![Page 100: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/100.jpg)
100
SubstringsSince C strings are pointers to characters, we can adjust the pointer to omit characters at the beginning.char chars[] = "racecar";char *str1 = chars;char *str2 = chars + 4;printf("%s\n", str1); // racecarprintf("%s\n", str2); // car
0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8'r' 'a' 'c' 'e' 'c' 'a' 'r' '\0'
str1
0xee0xf1 str2
0xd20xf5
![Page 101: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/101.jpg)
101
SubstringsSince C strings are pointers to characters, we can adjust the pointer to omit characters at the beginning. NOTE: the pointer still refers to the same characters!
char str1[] = "racecar";char *str2 = str1 + 4;str2[0] = 'f';printf("%s\n", str1);printf("%s\n", str2);
0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8'r' 'a' 'c' 'e' 'c' 'a' 'r' '\0'str1
str2
0xd20xf5
![Page 102: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/102.jpg)
102
SubstringsSince C strings are pointers to characters, we can adjust the pointer to omit characters at the beginning. NOTE: the pointer still refers to the same characters!
char str1[] = "racecar";char *str2 = str1 + 4;str2[0] = 'f';printf("%s\n", str1);printf("%s\n", str2);
0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8'r' 'a' 'c' 'e' 'f' 'a' 'r' '\0'str1
str2
0xd20xf5
![Page 103: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/103.jpg)
103
SubstringsSince C strings are pointers to characters, we can adjust the pointer to omit characters at the beginning. NOTE: the pointer still refers to the same characters!
char str1[] = "racecar";char *str2 = str1 + 4;str2[0] = 'f';printf("%s\n", str1); // racefarprintf("%s\n", str2); // far
0xf1 0xf2 0xf3 0xf4 0xf5 0xf6 0xf7 0xf8'r' 'a' 'c' 'e' 'f' 'a' 'r' '\0'str1
str2
0xd20xf5
![Page 104: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/104.jpg)
104
SubstringsTo omit characters at the end, make a new string that is a partial copy of the original.
// Want just "race"char str1[] = "racecar";
![Page 105: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/105.jpg)
105
SubstringsTo omit characters at the end, make a new string that is a partial copy of the original.
// Want just "race"char str1[] = "racecar";char str2[5];
![Page 106: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/106.jpg)
106
SubstringsTo omit characters at the end, make a new string that is a partial copy of the original.
// Want just "race"char str1[] = "racecar";char str2[5];strncpy(str2, str1, 4);
![Page 107: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/107.jpg)
107
SubstringsTo omit characters at the end, make a new string that is a partial copy of the original.
// Want just "race"char str1[] = "racecar";char str2[5];strncpy(str2, str1, 4);str2[4] = '\0';
![Page 108: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/108.jpg)
108
SubstringsTo omit characters at the end, make a new string that is a partial copy of the original.
// Want just "race"char str1[] = "racecar";char str2[5];strncpy(str2, str1, 4);str2[4] = '\0';printf("%s\n", str1); // racecarprintf("%s\n", str2); // race
![Page 109: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/109.jpg)
109
SubstringsWe can combine pointer arithmetic and copying to make any substrings we’d like.
// Want just "ace"char str1[] = "racecar";
![Page 110: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/110.jpg)
110
SubstringsWe can combine pointer arithmetic and copying to make any substrings we’d like.
// Want just "ace"char str1[] = "racecar";char str2[4];
![Page 111: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/111.jpg)
111
SubstringsWe can combine pointer arithmetic and copying to make any substrings we’d like.
// Want just "ace"char str1[] = "racecar";char str2[4];strncpy(str2, str1 + 1, 3);
![Page 112: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/112.jpg)
112
SubstringsWe can combine pointer arithmetic and copying to make any substrings we’d like.
// Want just "ace"char str1[] = "racecar";char str2[4];strncpy(str2, str1 + 1, 3);str2[3] = '\0';
![Page 113: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/113.jpg)
113
SubstringsWe can combine pointer arithmetic and copying to make any substrings we’d like.
// Want just "ace"char str1[] = "racecar";char str2[4];strncpy(str2, str1 + 1, 3);str2[3] = '\0';printf("%s\n", str1); // racecarprintf("%s\n", str2); // ace
![Page 114: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/114.jpg)
114
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans
![Page 115: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/115.jpg)
115
Announcements• Assignment 1 clarification: recursion, like loops, not allowed for SAT• Piazza is an official channel for course communication this quarter• We hope you enjoyed your first lab!• 3 minute break
![Page 116: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/116.jpg)
116
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans
![Page 117: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/117.jpg)
117
String Diamond• Write a function diamond that accepts a string parameter and prints its letters
in a "diamond" format as shown below.• For example, diamond("DAISY") should print:
DDADAIDAISDAISYAISYISYSYY
![Page 118: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/118.jpg)
118
String Diamond• Write a function diamond that accepts a string parameter and prints its letters
in a "diamond" format as shown below.• For example, diamond("DAISY") should print:
DDADAIDAISDAISYAISYISYSYY
![Page 119: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/119.jpg)
119
Daisy!
![Page 120: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/120.jpg)
120
Practice: Diamond
![Page 121: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/121.jpg)
121
Plan For Today• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and Spans
![Page 122: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/122.jpg)
122
Searching For Lettersstrchr returns a pointer to the first occurrence of a character in a string, or NULL if the character is not in the string.
char daisy[] = "Daisy";char *letterA = strchr(daisy, 'a');printf("%s\n", daisy); // Daisyprintf("%s\n", letterA); // aisy
If there are multiple occurrences of the letter, strchr returns a pointer to the first one. Use strrchr to obtain a pointer to the last occurrence.
![Page 123: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/123.jpg)
123
Searching For Stringsstrstr returns a pointer to the first occurrence of the second string in the first, or NULL if it cannot be found.
char daisy[] = "Daisy Dog";char *substr = strstr(daisy, "Dog");printf("%s\n", daisy); // Daisy Dogprintf("%s\n", substr); // Dog
If there are multiple occurrences of the string, strstr returns a pointer to the first one.
![Page 124: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/124.jpg)
124
String Spansstrspn returns the length of the initial part of the first string which contains only characters in the second string.
char daisy[] = "Daisy Dog";int spanLength = strspn(daisy, "Daeoi"); // 3
![Page 125: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/125.jpg)
125
String Spansstrcspn (c = “complement”) returns the length of the initial part of the first string which contains only characters not in the second string.
char daisy[] = "Daisy Dog";int spanLength = strcspn(daisy, "isdor"); // 2
![Page 126: CS107 Spring 2019, Lecture 4 - Stanford University...4 Plan For Today •Characters •Strings •Common String Operations •Comparing •Copying •Concatenating •Substrings •Break:](https://reader034.vdocuments.mx/reader034/viewer/2022050516/5fa05d3662b4474b22087aff/html5/thumbnails/126.jpg)
126
Recap• Characters• Strings• Common String Operations• Comparing• Copying• Concatenating• Substrings
• Break: Announcements• Practice: Diamond• More String Operations: Searching and SpansNext time: more strings