0413Strings.docx

advertisement
Strings: Chapter 11
Strings are arrays of characters with a special
symbol on the end which signifies to builtin
functions that the string has ended. Because
strings are arrays, only their starting address is
passed to a function, so it doesn’t know how
many characters are in that string until it finds
the end character
‘\n’ newline ascii 10
‘\0’ null ascii 0 – this is the character the builtin
finds
Anything else is a valid character in a string
#include <string.h>
4 builtins you should know:
first off, f/printf and f/scanf use %s to represent
a string variable
int strlen(char* str); // char* can be confusing
when we declare an array of ints, int[] but it’s
also int*, int[] implies treating as individual
elements
strings are declared with actual space
char word[20]; // but only treat as one
item
very rare that you would ever subscript into
a string to see a particular character.
when passed to functions, passed as char*
work
char word[10] = “hello”; // only time can use =
h e l
l
o \0
len = strlen(word); // 5, 6, or 10 answer 5
since you do not have the ability to use = for
assignment since strings are arrays, builtin is
char* strcpy(char* target, char* source);
// can ignore the char* return value
equivalent to target = source;
char word1[20] = “goodbye”, word2[20] =
“Hello”;
strcpy(word1, word2);
g
h
h
h
o
e
e
e
o
l
l
l
d
l
l
l
b
o
o
o
y e \0
\0
\0 e \0
\0
char* strcat(char* str1, char*str2);
// can ignore the char* return value
concatenates from 2 onto the end of 1
scanf(“%s%s”, fName, lName); strings already
addresses
fName: “Justin”
lName: “Gaudry”
strcat(fName, “ “); strcat(fName, lName);
int strcmp(char* str1, char* str2)
compares in increasing ASCII order resulting in
an integer code which you have to test to know
what order they’re in
if 1st < 2nd, return value is negative and test < 0
if 1st > 2nd , return value is positive and test > 0
if 1st == 2nd, return value is 0
help helps
-
helper help
+
Help help
-
help pleh
-
Mom dad
-
Download