Title: Strings Introduction
1Strings - Introduction
- A string is a null terminated character array
- terminated with an ASCII NULL character
- \0
- equivalent to decimal 0
- Rule Whenever you declare a character array,
always add one character for the NULL terminator
- Example
- define NAME_LEN 18
- char strNAME_LEN 1 \0
- strcpy( str, David M. Hutchison)
- strcpy automatically appends a NULL terminator
- without extra character, NULL overwrites
something
- Majority of string functions depend on NULL
terminator
- All located in
Lafore - Chapter 6197-200
219
2Strings - Introduction
- String Constant - any characters enclosed in
double quotation marks
- Includes the NULL terminator which terminates a
string
- NULL terminator is very important
- system uses the NULL terminator to detect the end
of a string.
- Examples
char str1 This is a string constant
char str2 This is also a string constant
Lafore - Chapter 6197-200
220
3String Variables
- Declared simply as an array of characters
- Declare character arrays size to be maximum
length of characters to store in the array 1
for the null terminator
- Declares fname to be a character array holding at
most 10 characters
- Note the missing in the call to scanf
- Remember, the name of an array is the same as
the address of the arrays 0th element
- scanf( s, fname0)
include define NAME_LEN 100 vo
id main( void) char fnameNAME_LEN 1
printf( Please enter your name )
gets( fname) printf( \n\nHello s.\n, fn
ame)
Lafore - Chapter 6197-200
221
4String Variables
In this example, gets automatically appended the
NULL terminator to the string
Can store at most 10 characters in the fname st
ring
Must leave space for the NULL terminator!
Unused bytes
Lafore - Chapter 6197-200
222
5Strings - Example
include define TAX_RATE 0.0825 d
efine NAME_LEN 50 void main( void) char
itemNameNAME_LEN 1 double price 0.0, t
ax 0.0, total 0.0 printf( \nEnter the
name of the item ) gets( itemName) pri
ntf( \nEnter the item price )
scanf( f, price) tax price TAX_R
ATE total price tax printf( \nIte
m s\n, itemName) printf( \nPrice 9.2f,
price) printf( \nTax 9.2f, tax) p
rintf( \ns, --------------------)
printf( \n-7s9.2f, Total, total)
N/A
223
6Strings - Example
- Output
- Note the use of format specifiers
- s represents a string
- string constant Total is left justified
- ---------------- is also a string constant
Enter the name of the item System Unit
Enter the item price 1459.95 Item System Un
it Price 1459.95 Tax 120.45 --------
------------ Total 1580.40
N/A
224
7Addresses Strings
- The name of the array is the same as the address
of the 0th element of the array
- Note the use of
include include def
ine STR_LEN 80 void main( void) char fn
ameSTR_LEN 1 int i 0
int length 0 puts( "Enter yo
ur name ") gets( fname)
length strlen( fname) for (
i 0 i printf( "Addr 5u char 'c\n",
fnamei, fnamei)
Lafore - Chapter 6205-206
225
8Addresses Strings
- Output
- Note
- Addresses characters occupy a single byte of
memory
- NULL character \0 has char
- Last three bytes of string are garbage
- purposely exceeded the bounds of the character
array
Enter your name Plato Addr 3486 char P
80 Addr 3487 char l 108 Addr 3488 cha
r a 97 Addr 3489 char t 116 Addr
3490 char o 111 Addr 3491 char 0
Addr 3492 char
35 Addr 3494 char u 117
Lafore - Chapter 6205-206
226
9Arrays of Strings
- Simply a two-dimensional array of characters
include include de
fine MAX 5 define LEN 40 void ma
in( void) int i 0 cha
r nameLEN 1 char listMAXLEN
1 "Dave", "Denise", "Cayte", "Chel
sea", "Tasha" printf( "\nEnter your name
") gets( name) fo
r ( i 0 i if ( strcmp( listi, name) 0)
printf( "Name was found.")
break
if ( i MAX) print
f( "Name was NOT found!")
listi refers to a single string in the array of
strings
strcmp returns 0 when two strings are equivilent.
Lafore - Chapter 6207-208
227
10Deleting Characters from a String
- Often useful to delete characters from the middle
of a string
- There is no string function that deletes from a
string
- Can be easily accomplished using the strcpy()
function
- Remember, the name of a string (character array)
is simply the starting address of the string
- char nameStr David M. Hutchison
- nameStr9 would reference the substring
Hutchison
- this is simply the address of a substring
- program considers this a string
- valid starting address
- terminated with a NULL character
- Can simply use the format specifier of s
- printf( s s\n, nameStr, nameStr9)
- David M. Hutchison Hutchison
Lafore - Chapter 6208-210
228
11Deleting Characters from a String
include include
define STR_LEN 80 void strdel( char
str, int n) void main( void) char st
ringSTR_LEN 1 int position 0
printf( "\nEnter string, then enter po
sition\n") gets( string) sc
anf( "d", position) strdel( string, posit
ion) puts( string)
void strdel( char str, int n) strcpy(
strn, strn1)
puts() is a primitive means of printing a string
Note the use of to copy a substring up a
position, effectively deleting a single character
in the original string.
Lafore - Chapter 6208-210
229
12Strings scanf
- scanf has serious limitations when used with
strings
- Uses any whitespace character to terminate entry
of a string
- scanf reads the space character between David
and M and terminates the input
- There is no easy way, using scanf, to accept
multiple word input into a single string
- Solution
- Use gets() - get string
- Specializes in reading strings
- Input is terminated when the Return key is
pressed
- Embedded whitespace characters are stored in the
string
Enter your name David M. Hutchison Hello David.
230
13Strings gets
- Use gets() to read a complete string
- Ensure the string is large enough to store the
input string
include define NAME_LEN 100 vo
id main( void) char fnameNAME_LEN 1
printf( Please enter your name (at most 100
characters) ) gets( fname) printf( \n
\nHello s.\n, fname) Please enter your
name (at most 100 characters) David M.
Hutchison Hello David M. Hutchison.
231
14String Functions - strcpy()
- char strcpy(char str1, char str2)
- Description
- Copies string str2 to string str1, including the
NULL terminator
- Returns
- str1 - return value str1 not used frequently
- Example
include include define
STR_LEN 100 char str1STR_LEN 1 \0
char str2STR_LEN 1 \0
strcpy( str1, David M. Hutchison) strcpy( st
r2, str1) printf( s\ns, str1, str2)
David M. Hutchison David M. Hutchison
Lafore - Chapter 6, KR249
232
15String Functions - strncpy()
- char strncpy(char str1, char str2, int n)
- Description
- Copies n characters of string str2 to string
str1, NOT including the NULL terminator
- Returns
- str1 - return value str1 not used frequently
- Example
include include define
STR_LEN 100 char str1STR_LEN 1 \0
char str2STR_LEN 1 \0
strcpy( str1, David M. Hutchison) strncpy( s
tr2, str1, 5) str25 \0 / NUL
L TERMINATOR !!!!! / printf( s\ns, str1, s
tr2) David M. Hutchison David
Lafore - Chapter 6, KR249
233
16String Functions - strcat()
- char strcat(char str1, char str2)
- Description
- Concatenates (appends) str2 onto the end of str1
- Ensure str1 is large enough to hold
concatenation
- Returns
- str1 - return value str1 not used frequently
- Example
include include define
STR_LEN 100 char str1STR_LEN 1 \0
char str2STR_LEN 1 \0
strcpy( str1, David M.) strcpy( str2, Hutc
hison) strcat( str1, str2) printf( s, st
r1) David M. Hutchison
Lafore - Chapter 6, KR249
234
17String Functions - strncat()
- char strncat(char str1, char str2, int n)
- Description
- Concatenates (appends) n characters from the
beginning of str2 onto the end of str1 placing
a NULL terminator on str1
- Returns
- str1 - return value str1 not used frequently
- Example
include include define
STR_LEN 100 char str1STR_LEN 1 \0
char str2STR_LEN 1 \0
strcpy( str1, David M.) strcpy( str2, Hutc
hison) strncat( str1, str2, 6) printf( s
, str1) David M. Hutch
Lafore - Chapter 6, KR249
235
18String Functions - strcmp()
- int strcmp(char str1, char str2)
- Description
- Compares str1 to str2
- Returns
- 0 if str1 is equal to str2
- 0 if str1 str2
- Example
include include char st
r1STR_LEN 1 \0 char str2STR_LEN 1
\0 strcpy( str1, CS 209) strcpy( str2,
CS 209) if ( strcmp( str1, str2) 0) pr
intf( The strings s and s are EQUAL!\n, str1,
str2) else printf( The strings s and
s are NOT equal!\n, str1, str2)
The strings CS 209 and CS 209 are EQUAL!
Lafore - Chapter 6, KR249
236
19String Functions - strncmp()
- int strncmp(char str1, char str2, int n)
- Description
- Compares n characters from the beginning of
str2 to str1
- Returns
- 0 if str1 is equal to str2
- 0 if str1 str2
- Example
include include char st
r1STR_LEN 1 \0 char str2STR_LEN 1
\0 strcpy( str1, CS 209) strcpy( str2,
CS 209 - Computer Programming for Business with
C) if ( strncmp( str1, str2, 6) 0) pri
ntf( The tested strings are EQUAL!\n)
else printf( The tested strings are NOT e
qual!\n) The tested strings are EQUAL!
Lafore - Chapter 6, KR249
237
20String Functions - strlen()
- size_t strlen(char str)
- Description
- Determines the length of str
- Returns
- Unsigned integer (size_t) value representing the
length of str
- Example
include include char st
r "This is a test message..."
int len 0 len strlen( str) printf( The
length of s is d.\n", str, len)
The length of This is a test message is 25.
Lafore - Chapter 6, KR250
238
21String Traps Pitfalls
- Forgetting the NULL terminator
- Example
- Solution
- Use a function that appends the NULL, like
strcpy
- If not, manually append NULL
char str4 \0 / NO SPACE FOR NULL
TERMIANTOR!! / str0 T str1 E str
2 S str3 T printf( String s\n
, str) / ERROR /
char str5 \0 / Space for NULL
terminator / strcpy( str, TEST) / Appends
NULL terminator / printf( String s\n, str
)
char str5 \0 / Space for NULL
terminator / ... str4 \0 / A
ppend NULL terminator /
N/A
239
22String Traps Pitfalls
- Using instead of strcmp()
- Example
- Compares the addresses of the start of the
strings, which will never be equal!
- Solution
- Use strcmp()
char buf1 Dave char buf2 Hutchison
if ( buf1 buf2) printf( Strings are
equal\n) / ERROR /
char buf1 Dave char buf2 Hutchison
if ( strcmp( buf1, buf2) 0) printf(
Strings are equal\n) / ERROR /
N/A
240
23String Traps Pitfalls
- Be careful using scanf() and gets() together
- scanf leaves the newline character in the
keyboard buffer
- use fflush( stdin)
- One alternative is to treat everything like a
string and convert as necessary
- Use atoi(), atof(), etc. or sscanf() to convert
characters from strings to numbers
- You must include for the ato
functions
- sscanf is in
241