Title: Chapter 10 Strings and Pointers
1Chapter 10 Strings and Pointers
2Introduction
- String Constant
- Example printf(Hello)
- Hello a string constant
- A string constant is a series of characters
surrounded by double quotes. - How to declare a variable to store string values?
- Represent a string using a one-dimensional array
of type char - char stringsize
- Question The size of a character array is fixed,
how can this variable take string constants with
different lengths as values?
3Outline
- String
- Representation of a string \0
- Using scanf to read in string
- Initilization of strings
- String-Handling Functions in the Standard Library
- Passing Arguments to main() using an array of
strings
4The End-of-String Sentinel \0
- A string is
- a one-dimensional array of type char.
- char w100
- character value \0 is used to terminate a string
- strings have a variable length delimited by the
null character \0 but with a maximum length
determined by the size of the character array - The size of the string must include the storage
needed for the null character \0.
5The End-of-String Sentinel \0
the null character value \0 is used to terminate
a string
include ltstdio.hgt int main(void) char
w100 w0'A' w1'B'
w2'C' w3'\0' printf("s\n", w)
include ltstdio.hgt int main(void) char
w100 w0'A' w1'B'
w2'C' w3'\0' w4D'
printf("s\n", w)
a.out ABC
a.out ABC
6The End-of-String Sentinel \0
The size of the string must include the storage
needed for the null character \0.
include ltstdio.hgt int main(void) char
w3 w0'A' w1'B' w2'C'
w3'\0' printf("s\n", w)
overrun the bounds of w
7Outline
- String
- Representation of a string \0
- Using scanf to read in string
- Initilization of strings
- String-Handling Functions in the Standard Library
- Passing Arguments to main() using an array of
strings
8Using scanf to reading string
- Using scanf to read in a string
- scanf(s, w)
- read in non-white space characters
- positions the input stream to an initial
non-white space character - read in non-white space characters
- The process stops when a white space character or
EOF is encountered. - a null character is placed in memory to end the
string.
9Using scanf to reading string
- scanf(s,w)
- read in non-white space characters
- positions the input stream to an initial
non-white space character - read in non-white space characters
- The process stops when a white space character or
EOF is encountered. - a null character is placed in memory to end the
string.
include ltstdio.hgt int main(void) char
w10 printf("Enter strings\n", w)
scanf("s", w) printf("s\n", w)
a.out Enter strings Hello Hello
a.out Enter strings Hello World Hello
10Outline
- String
- Representation of a string \0
- Using scanf to reading string
- Initilization of strings
- String-Handling Functions in the Standard Library
- Passing Arguments to main() using an array of
strings
11Initialization of Strings
- Initialization of Strings
- Example initialize a string variable as abc
- char s a, b, c, \0
- char sabc
include ltstdio.hgt int main(void) char
w"abc" printf("d\n", sizeof(w))
a.out 4
The size of the string must include the storage
needed for the null character \0.
12Initialization of Strings
- A pointer to char can also be initialized with a
constant string. - A string constant is stored in memory by the
compiler. - the pointer is assigned the address of the
constant string in memory. - Example char p abc
include ltstdio.hgt int main(void) char
p"abc" printf("s\n",p)
a.out abc
13Initialization of Strings
- Difference between
- initializing an array with a constant string and
- initializing a pointer with a constant string
include ltstdio.hgt int main(void) char
s"abcdefg" char p"abcdefg"
printf("s\n",s) printf("s\n",p)
printf("d\n",sizeof(p)) printf("d\n",sizeof
(s))
a.out abcdefg abcdefg 4 8
4 bytes is used to represent a memory address
14Initialization of Strings
- Difference between
- initializing an array with a constant string
- the array contains the individual characters
followed by the null character - initializing a pointer with a constant string
- A string constant is stored in memory by the
compiler. - the pointer is assigned the address of the
constant string in memory.
15Examples Process a string using array notation
with subscripts
include ltstdio.hgt int main(void) char c,
name100 int i printf("Enter a
Message\n") for (i0 (cgetchar())!'\n'
i) nameic namei'\0'
for (i0 namei!'\0' i)
if(isupper(namei))
nameitolower(namei) else
if(islower(namei))
nameitoupper(namei)
printf("\ns\n", name)
a.out Enter a Message Have a Good Day! hAVE
A gOOD dAY!
16Examples Process a string using pointer
include ltstdio.hgt int main(void) char c,
name100, p int i printf("Enter a
Message\n") for (i0 (cgetchar())!'\n'
i) nameic namei'\0'
for (pname p!'\0' p)
if(isupper(p)) ptolower(p)
else if(islower(p))
ptoupper(p) printf("\ns\n",
name)
a.out Enter a Message Have a Good Day! hAVE
A gOOD dAY!
17Examples Process a string using pointer
include ltstdio.hgt int main(void) char s
"Hello World" printf("s\n", s)
printf("s\n", s1) printf("s\n", s2)
a.out Hello World ello World llo World
18Outline
- String
- String-Handling Functions in the Standard Library
- Passing Arguments to main() using an array of
strings
19String-Handling Functions in the Standard Library
- String-handling functions
- Function prototypes are provided by string.h
- include ltstring.hgt
- Functions
- Concatenate two strings strcat (s1, s2)
- Compare two strings int strcmp (s1, s2)
- Copy s2 to s1 strcpy (s1, s2)
- Length of a string strlen (s)
20String-Handling Functions in the Standard Library
- char strcat (char s1, const char s2)
- Concatenates s1 and s2, the result is put in s1.
The string s1 is returned.
include ltstdio.hgt include ltstring.hgt int
main(void) char s1100 "Good Day" char
s2100 "Hello World" strcat(s1, s2)
printf("s2s, s1 s\n", s2, s1) strcat(s1,
s26) printf("s2s, s1 s\n", s2, s1)
a.out s2Hello World, s1 Good DayHello
World s2Hello World, s1 Good DayHello WorldWorld
21String-Handling Functions in the Standard Library
- int strcmp (const char s1, const char s2)
- An integer is returned that is less than, equal
to, or greater tan zero, depending on whether s1
is lexicographically less than, equal to, or
greater than s2
include ltstdio.hgt include ltstring.hgt int
main(void) char s1100 "Good Day" char
s2100 "Hello World" printf("d\n",
strcmp(s1, s2)) printf("d\n", strcmp(s1,
s1)) printf("d\n", strcmp(s2, s1))
a.out -1 0 1
22String-Handling Functions in the Standard Library
- char strcpy (char s1, const char s2)
- s2 is copied into s1 until \0 is moved. Whatever
exists in s1 is overwritten. - It is assumed that s1 has enough space to hold
the result. - The value s1 is returned.
include ltstdio.hgt include ltstring.hgt int
main(void) char s1100 "Good Day" char
s2100 "Hello World" strcpy(s1, s2)
printf("s2s, s1 s\n", s2, s1)
strcpy(s11, s2) printf("s2s, s1 s\n",
s2, s1) strcpy(s11, s26) printf("s2s,
s1 s\n", s2, s1)
a.out s2Hello World, s1 Hello World s2Hello
World, s1 HHello World s2Hello World, s1 HWorld
23String-Handling Functions in the Standard Library
- unsigned strlen (const char s)
- A count of the number of characters before \0 is
returned.
include ltstdio.hgt include ltstring.hgt int
main(void) char s1100 "Good Day"
printf("d\n", strlen(s1))
a.out 8
24Outline
- String
- String-Handling Functions in the Standard Library
- Passing Arguments to main() using an array of
strings
25Passing Arguments to main()
- How main() communicates with the operating
system? - int main(void)
- int main( int argc, char argv)
- argc the number of the command line arguments
- argv an array of strings
26Passing Arguments to main()
include ltstdio.hgt int main(int argc, char
argv) int i printf("d \n", argc)
for (i0 i lt argc i) printf("s\n",
argvi)
a.out Hello World 3 a.out Hello World
27Summary
- String
- Representing a string using an array of
characters - \0 is used to terminated a string
- ? strings have a variable length delimited by the
null character \0 but with a maximum length
determined by the size of the character array - initialization of strings
- String-Handling Functions in the Standard Library
- Passing Arguments to main()
- argc number of arguments
- argv an array of strings