C programming---String - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

C programming---String

Description:

Title: C programming---basic Last modified by: jigsaw Document presentation format: (4:3) Other titles: Arial Calibri Office C programming ... – PowerPoint PPT presentation

Number of Views:262
Avg rating:3.0/5.0
Slides: 29
Provided by: gsu104
Category:

less

Transcript and Presenter's Notes

Title: C programming---String


1
C programming---String
2
String Literals
A string literal is a sequence of characters
enclosed within double quotes hello world
3
Escape 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
4
Continuing 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)
5
How 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

6
How 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.
7
Operations on String Literal
char p abc char ch abc1 char
digit_to_hex_char(int digit)
0123456789ABCDEFdigit
8
Operations 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)
9
String 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
10
String 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
11
Character Arrays V.S. Character Pointers
char data june 14 char data june 14
12
Character 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?
13
Reading 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

14
Reading 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
15
Accessing 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
16
Using the C String Library
char str10, str210 str1 abc //
WRONG str2 str1 // WRONG if(str1 str2)
// WRONG
ltstring.hgt
17
The 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)
18
The strlen(String Length) Function
size_t strlen(const char s) int len
strlen(abc)
19
The 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)
20
The 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

21
The strcmp(String Comparison) Function
  1. The characters in each of the sequences A-Z,
    a-z, and 0-9 have consecutive codes.
  2. All upper-case letters are less than all
    lower-case letters A-Z(65-90)
    a-z(97-122)
  3. Digits are less than letters 0-9(48-57)
  4. Spaces are less than all printing characters 32
    in ASCII

22
String 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
23
String 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
24
String Idiom
while(s) s while(s)
25
String Idiom
Copying a String
char strcat(char s1, const char s2)
char p s1 while(p) p
while(p s) // idiom
return s1
26
Array of Strings
char planets8 Mercury, Venus,
Earth,
Mars, Jupiter, Saturn,
Uranus, Neptune,
Pluto char planets Mercury,
Venus, Earth,
Mars, Jupiter, Saturn,
Uranus, Neptune,
Pluto
27
Array of Strings








Mercury\0
Venus\0
Earth\0
Mars\0
Jupiter\0



28
Command-Line Arguments
argv



Program name
-l\0
remind.c\0
int main(int argc, char argv) .
Write a Comment
User Comments (0)
About PowerShow.com