Title: Character math
1Character math
- Recall that C uses integers to encode characters
- example 'A' is encoded as 65 (in ASCII)
- This means that math on characters will work
- Example 1 (how NOT to use character math)
printf("'A''lt' is c", 'A''lt' ) Output
'A''lt' is
'A' is 65 'lt' is 60 '' is 125
2Character math
- Math on characters will work
- Example 2 alphabetic comparisons (a good use
for character math)In ASCII, digits lt UPPERCASE
lt lowercase
char c1, c2 scanf(" cc", c1, c2) if (c1 lt
c2) printf("sorted c, then c\n", c1,
c2) else printf("sorted c, then c\n", c2,
c1)
3Character functions
- ctype.h contains several useful character
functions - int isalpha(char c) / Is it a letter? /
- int ispunct(char c) / A punctuation mark? /
- int isspace(char c) / Is it whitespace? /
- int islower(char c) / Is it lowercase? /
- int isupper(char c) / Is it uppercase? /
- int isdigit(char c) / Is it a digit? /
- char toupper(char c) / Convert to uppercase /
- char tolower(char c) / Convert to lowercase /
4Strings
- A string is a null-terminated array of
characters. - What does this mean?
- A string is a sequence of characters treated as a
unit - The very last character in a string is the null
character (ASCII value zero) '\0' . It is
used as a delimiter to identify where the string
ends. - String examples
- "asldk2 2_at_ 2 _VA12 78 "
- " The night has d eyes\n "
- "?THIS is not my beautiful life!-gtTalkingHeads"
- The null character is not printable!
5Strings
is NOT A STRING because it is not null-terminated
'J' 'o' 'h' 'n'
'J' 'o' 'h' 'n' '\0'
is A STRING
6Declaring initializing strings
- When declaring an array that will hold a string,
we must be careful to make it large enough to
hold the string INCLUDING the null character. - Usually, we make the array larger than what we
need. - The unused characters are either null or junk
values.
7Initializing strings
- Rather tedious methods to initialize a string,
especially if it is long - char name20 'J', 'o', 'h', 'n', '\0'
- char name20name0 'J'name1
'o'name2 'h'name3 'n'name4
'\0'
8Initializing strings
- char name20scanf("s", name)
- scanf will only read up until it sees whitespace.
- It will automatically null-terminate the string.
- char firstname20. lastname20scanf("s s",
firstname, lastname) - Note that we don't have to use an ampersand ()
because the name of the array is already the
address where the array begins.
9Initializing strings
- char name4scanf("s", name)
- This is used for formatted input.
- Normally, scanf does not check for array
overflow. - We will do an example in class to demonstrate
what may happen - Possible solution Use a width specifier to limit
the number of characters to be read scanf("3s",
name) will read only 3 characters into name. The
rest stay in the input buffer. - See textbook for more examples.
10Initializing strings
- char name40fgets(name, 40, stdin)
- It reads up to 40-1 characters from standard
input and places them in name. - If you type fewer than 39 characters, it will
place them all (including the newline you type at
the end) into name and add a null character to
the end. - If you type more than 39 characters, it will
place the first 39 into name, add a null
character to the end, and the remaining
characters will stay in the input buffer.
11Initializing strings
- Both fgets() and scanf() may leave characters in
the input buffer. - Using fgets() and scanf() together in the same
program is not recommended. - There is no standard way to flush the input
buffer.
12Working with strings
- The string.h library contains several useful
string functions
first n chars
all chars
strlen()
Find string length
strncpy()
strcpy()
Copy a string
strncat()
strcat()
Join two strings
strncmp()
strcmp()
Compare two strings
later
strrchr()
strchr()
Find char in string
strstr()
Find string in string
13strlen()
- Takes as argument a string and returns the number
of characters in it (not counting the null
character)
includeltstring.hgt int main () char
message10 "Hello" int length
strlen(message) printf("d", length) return
0
5
output
14strcpy()
- Takes as arguments two strings and copies the
value of the second string into the first one. - You must make sure the first string is big
enough.
includeltstring.hgt int main () char
message10 "Hello" char
copy10 strcpy(copy, message) printf("s",
copy) return 0
Hello
output
15strncpy()
- Takes as arguments two strings and an integer n
and copies the first n characters of the second
string into the first one.
includeltstring.hgt int main () char
message10 "Hello" char
copy10 strncpy(copy, message,
3) printf("s", copy) return 0
Hel
output
16strcat()
- Takes as arguments two strings and appends the
second string to the first one (thus changing the
value of the first string)
make sure there's enough room for the new string
that will be created
includeltstring.hgt int main () char word110
"Base" char word210 "ball" strcat(wo
rd1, word2) printf("s, s", word1,
word2) return 0
Baseball, ball
output
17strncat()
- Similar to strcat but it appends only the first n
characters of the second string to the first one.
make sure there's enough room for the new string
that will be created
includeltstring.hgt int main () char word110
"Base" char word210 "ballroom" strnc
at(word1, word2, 4) printf("s",
word1) return 0
Baseball
output
18strcmp()
- Compares two strings lexicographically and
returns -1 if the first is "smaller", 1 if the
first is "larger", 0 if they are the same.
includeltstring.hgt int main () char word110
"base" char word210 "ballroom" if(
strcmp(word1, word2) 1) printf("s gt s\n",
word1, word2) return 0
base gt ballroom
output
19strcmp()
- strcmp() compares the strings one character at a
time (using the ASCII values of the characters
for the comparison). - In the previous example, "base" is compared to
"ballroom" as follows - 'b' 'b'
- 'a' 'a'
- 's' gt 'l' gt return 1
20strncmp()
- Similar to strcmp() but it only compares the
first n characters.
includeltstring.hgt int main () char word110
"base" char word210 "ballroom" int
diff strncmp(word1, word2, 2) printf("d",
diff) return 0
0
output
21More string functions
- The following functions will be discussed after
we have covered pointers - strchr()
- strstr()
- strtok()