Title: C programming---String
1C programming---String
2String Literals
A string literal is a sequence of characters
enclosed within double quotes hello world
3Escape Sequence in String Literals
Candy\n Is dandy\n But liquor\n Is quicker.\n
Candy Is dandy But liquor Is quicker.
\1234 contains two characters \123 and
4 \189 contains three characters \1 , 8
and 9 \x0 . \xff hex escape
4Continuing a String Literal
printf(When you come to a fork in the road,
\ take it --- Yogi Berra) Wrecking the
programs indented structure printf( When you
come to a fork in the road, take
it --- Yogi Berra)
5How String Literals Are Stored
- When we call printf and supply a string literal
as an argument, what are we actually passing? - C treats string literals as character arrays.
- A string literal of length n will be stored in a
n 1 bytes memory. - The last character in the array is the null
character as a marker for the end of the string
6How String Literals Are Stored
null character \0 Do not confuse it with 0
Since a string literal is stored as an array, the
compiler treats it as a pointer of type char
. So when printf(abc) is called, the address
of abc is passed.
7Operations on String Literal
char p abc char ch abc1 char
digit_to_hex_char(int digit)
0123456789ABCDEFdigit
8Operations on String Literal
char p abcd p o a is different
from a a is represented by a pointer to a
memory address a is represented by an
integer printf(a) printf(a)
9String Variables
Java has a String type, but C does not. Any
one-dimensional array of characters can be used
to store a string. There is no faster way to
determine the length of a string than a
character-by-character search for \0
10String Variable
define STR_LEN 90 .. char strSTR_LEN
1 char data18 June 14 char data29
June 14 char data37 June 14
char data4 June 14
J u n e 1 4 \0
J u n e 1 4 \0 \0
J u n e 1 4
11Character Arrays V.S. Character Pointers
char data june 14 char data june 14
12Character Arrays V.S. Character Pointers
char strSTR_LEN 1, p p str In this way
you can manipulate string with pointers p0
o p1 r But char p p0 a p1
b p2 c THIS IS WRONG BECAUSE?
13Reading and Writing Strings
- Writing strings with printf and puts
- printf(s, str)
- puts(str)
- Reading strings with scanf and gets
- scanf(s, str) //skip white space
- gets(str) //doesnt skip white space before
starting to read the string - //reads until it finds a new-line
character
14Reading Strings Character by Character
int read_line(char str, int n) int ch, i
0 while((ch getchar()) ! \n)
if(i lt n) stri ch
stri \0 return i
15Accessing the Characters in a String
int count_spaces(const char s) int
count 0, i for(i0 si!\0 i)
if(si ) count
return count
int count_spaces(const char s) int count
0 for( s!\0 s) if(s
) count return count
16Using the C String Library
char str10, str210 str1 abc //
WRONG str2 str1 // WRONG if(str1 str2)
// WRONG
ltstring.hgt
17The strcpy(String Copy) Function
char strcpy(char s1, const char s2) str2
abcd strcpy(str2, abcd) strcpy(str1,
str2) strcpy(str1, strcpy(str2, abcd)) It
is safer to call strncpy, but it is slower. char
strncpy(char s1, const char s2, size_t s)
18The strlen(String Length) Function
size_t strlen(const char s) int len
strlen(abc)
19The strcat (String Concatenation) Function
char strcat(char s1, const char
s2) strcpy(str1, abc) strcat(str1,
def) char str16 abc strcat(str1,
def) /wrong/ strncat(s1, s2,
strlen(s1)-strlen(s2)-1)
20The strcmp(String Comparison) Function
- int strcmp(const char s1, const char 2)
- if(strcmp(s1, s2) lt 0)
- if(strcmp(s1, s2) lt 0)
- strcmp compares strings based on their
lexicographic ordering, which resembles the way
words are arranged in a dictionary - The first i characters of s1 and s2 match, but
the (i1)st character of s1 is less than the
(i1)st character of s2. abc lt bcd abd
lt abe - All characters of s1 match s2, but s1 is shorter
than s2. abc lt abcd
21The strcmp(String Comparison) Function
- The characters in each of the sequences A-Z,
a-z, and 0-9 have consecutive codes. - All upper-case letters are less than all
lower-case letters A-Z(65-90)
a-z(97-122) - Digits are less than letters 0-9(48-57)
- Spaces are less than all printing characters 32
in ASCII
22String Idioms
Searching for the End of a String
size_t strlen(const char s) size_t n
for(n0 s!\0 s) n
return n size_t strlen(const char s)
size_t n 0 for( s!\0 s)
n return n
size_t strlen(const char s) size_t n
0 for( s s) n return
n size_t strlen(const char s) size_t
n 0 for( s ) n
return n
23String Idiom
Searching for the End of a String
size_t strlen(const char s) size_t n
0 while(s) n return
n
size_t strlen(const char s) const char p
s while(s) s return s
- p
24String Idiom
while(s) s while(s)
25String Idiom
Copying a String
char strcat(char s1, const char s2)
char p s1 while(p) p
while(p s) // idiom
return s1
26Array of Strings
char planets8 Mercury, Venus,
Earth,
Mars, Jupiter, Saturn,
Uranus, Neptune,
Pluto char planets Mercury,
Venus, Earth,
Mars, Jupiter, Saturn,
Uranus, Neptune,
Pluto
27Array of Strings
Mercury\0
Venus\0
Earth\0
Mars\0
Jupiter\0
28Command-Line Arguments
argv
Program name
-l\0
remind.c\0
int main(int argc, char argv) .