13. Strings - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

13. Strings

Description:

A string literal may be extended over more than one line by writing ... No ampersand is needed when using scanf to read into a string variable. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 24
Provided by: Ritwik3
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: 13. Strings


1
13. Strings
2
String Literals
String literals are enclosed in double
quotes "Put a disk in drive A, then press any
key to continue\n A string literal may be
extended over more than one line by writing \
immediately followed by the end of the
line printf("Put a disk in drive A, then \ press
any key to continue\n") A string literal may
be divided into two or more shorter strings the
compiler will join these together into one
string printf("Put a disk in drive A, then
" "press any key to continue\n")
3
How String Literals Are Stored
The string literal "abc" is represented by the
three characters a, b, and c, followed by a null
character (\0) Like any array, a string
literal is represented by a pointer to the first
character in the string. A string literal of
length 1 is different from a character constant.
A string literal of length 1 ("a", for example)
is represented by a pointer. A character constant
('a', for example) is represented by an integer
value.
4
How String Literals Are Stored
Warning Dont ever use a character constant
when a string literal is required (or
vice-versa). The call printf("\n") is legal,
because printf expects a string as its first
parameter, but printf('\n') is not.
5
String Variables
A string variable is just a one-dimensional
array of characters define STR_LEN 80 char
strSTR_LEN1 The array should be one
character longer than the string it will hold, to
leave space for the null character. Warning
Failure to leave room for the null character may
cause unpredictable results when using
string-handling functions in the C library.
6
String Variables
A string variable can be initialized char
date18 "June 14" The date array will have
the following appearance A string
initializer need not completely fill the
array char date29 "June 14" The leftover
array elements are filled with null characters
7
String Variables
If the length of the array is omitted, the
compiler will compute it char date3 "June
14" / date3 is 8 characters long /
8
Reading and Writing Strings
To read or write a string, use scanf or printf
with the s conversion specification scanf("s",
str) printf("s", str) scanf skips white space,
then reads characters into str until it
encounters a white-space character. No
ampersand is needed when using scanf to read into
a string variable. Since a string variable is an
array, the name of the variable is already a
pointer (to the beginning of the array). A
faster alternative Use gets and puts instead of
scanf and printf gets(str) puts(str) gets
reads characters into str until it encounters a
new-line character. puts prints str, followed by
a new-line character.
9
Reading and Writing Strings
A faster alternative Use gets and puts instead
of scanf and printf gets(str) puts(str) gets
reads characters into str until it encounters a
new-line character. puts prints str, followed by
a new-line character. scanf and gets
automatically put a null character at the end of
the input string. printf and puts assume that the
output string ends with a null character.
10
Reading and Writing Strings
Warning Both scanf and gets assume that the
string variable is large enough to contain the
input string (including the null character at the
end). Failing to make the variable long enough
will have unpredictable results. To make scanf
safer, use the conversion specification ns,
where n specifies the maximum number of
characters to be read. Use fgets instead of gets
for greater safety.
11
Reading and Writing Strings
To read or write a string, use scanf or printf
with the s conversion specification scanf("s",
str) printf("s", str) scanf skips white
space, then reads characters into str until it
encounters a white-space character. No
ampersand is needed when using scanf to read into
a string variable. Since a string variable is an
array, the name of the variable is already a
pointer (to the beginning of the array).
12
Reading and Writing Strings
A faster alternative Use gets and puts instead
of scanf and printf gets(str) puts(str) gets
reads characters into str until it encounters a
new-line character. puts prints str, followed by
a new-line character. scanf and gets
automatically put a null character at the end of
the input string. printf and puts assume that the
output string ends with a null character.
13
Reading and Writing Strings
Warning Both scanf and gets assume that the
string variable is large enough to contain the
input string (including the null character at the
end). Failing to make the variable long enough
will have unpredictable results. To make scanf
safer, use the conversion specification ns,
where n specifies the maximum number of
characters to be read. Use fgets instead of gets
for greater safety.
14
Accessing the Characters in a String
Because of the close relationship between
arrays and pointers, strings can be accessed
either by array subscripting or by pointer
reference. Array version of a function that
counts the number of spaces in a string int
count_spaces(const char s) int count,
i count 0 for (i 0 si ! '\0' i) if
(si ' ') count return count
15
Accessing the Characters in a String
Pointer version of the same function int
count_spaces(const char s) int count count
0 for ( s ! '\0' s) if (s ' ')
count return count
16
Using the C String Library
C provides little built-in support for strings.
Since strings are treated as arrays, they are
restricted in the same ways as arraysin
particular, strings cannot be copied or
compared. Warning Attempts to copy or compare
two strings using Cs built-in operators will
fail char str110, str210 str1 str2 /
illegal / if (str1 str2) ... / will produce
the wrong result / The C library provides a
rich set of functions for performing operations
on strings. Declarations for these functions
reside in ltstring.hgt.
17
The strcpy Function
strcpy copies one string into another char
str110, str210 strcpy(str1, "abcd") / str1
now contains "abcd" / strcpy(str2, str1) /
str2 now contains "abcd" / strcpy calls can
be chained strcpy(str2, strcpy(str1,
"abcd")) / both str1 and str2 now contain
"abcd" / Warning strcpy has no way to check
that the second string will fit in the first one.
18
The strcat Function
strcat appends the contents of one string to
the end of another char str10
"abc" strcat(str, "def") / str now contains
"abcdef" / Warning strcat has no way to
check that the first string can accommodate the
added characters.
19
The strcmp Function
strcmp compares two strings if (strcmp(str1,
str2) lt 0) ... strcmp returns a value less than,
equal to, or greater than 0, depending on whether
str1 is less than, equal to, or greater than
str2. strcmp considers str1 to be less than
str2 if The first i characters of str1 and str2
match, but the (i1)st character of str1 is less
than the (i1)st character of str2 (for example,
"abc" is less than "acc", and "abc" is less than
"bcd"), or All characters of str1 match str2,
but str1 is shorter than str2 (for example, "abc"
is less than "abcd")
20
The strlen Function
strlen returns the length of a string int
i char str10 i strlen("abc") / i is now 3
/ i strlen("") / i is now 0 / strcpy(str,
"abc") i strlen(str) / i is now 3 / When
given an array of characters as its argument,
strlen does not measure the length of the array
itself instead, it returns the length of the
string stored inside the array.
21
Writing the strlen Function
Version 1 int strlen(const char s) int
n for (n 0 s ! '\0' s) n return
n Version 2 int strlen(const char
s) int n 0 for ( s ! 0 s) n return
n
22
Writing the strlen Function
Version 3 int strlen(const char s) int n
0 for ( s s) n return n Version
4 int strlen(const char s) int n 0 while
(s) n return n
23
Writing the strcat Function
char strcat(char s1, const char s2) char p
s1 while (p) --p while (p
s2) return s1
Write a Comment
User Comments (0)
About PowerShow.com