第10章 字符串和字符串函数

27
第 10 第 第第第第第第第第第

Upload: galena

Post on 18-Jan-2016

73 views

Category:

Documents


5 download

DESCRIPTION

第10章 字符串和字符串函数. · 函数: · 创建和使用字符串。 · 利用 C 库里的字符串和字符串函数创建你自己的字符串函数。 · 使用命令行参数。. 1 字符串表示和字符串 I/O. 字符串( character string) 是以空字符( \0 )结尾的 char 数组 11.1 strings.c 11.2 quotes.c. 二 · 字符串数组及其初始化. 三 · 数组与指针. 四 · 数组和指针的差别. char heart[] = "I love tillie!."; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第10章 字符串和字符串函数

第 10 章 字符串和字符串函数

Page 2: 第10章 字符串和字符串函数

· 函数: · 创建和使用字符串。 · 利用 C 库里的字符串和字符串函数创建

你自己的字符串函数。 · 使用命令行参数。

23/4/21

Page 3: 第10章 字符串和字符串函数

1 字符串表示和字符串 I/O 字符串( character string) 是以空字符

( \0 )结尾的 char 数组 11.1 strings.c 11.2 quotes.c

23/4/21

Page 4: 第10章 字符串和字符串函数

二 · 字符串数组及其初始化

23/4/21

Page 5: 第10章 字符串和字符串函数

三 · 数组与指针

23/4/21

Page 6: 第10章 字符串和字符串函数

四 · 数组和指针的差别 char heart[] = "I love tillie!."; char *hear = "I love Millie!.";

23/4/21

Page 7: 第10章 字符串和字符串函数

五 · 字符串数组 const char *mytal[LIM] = {"Adding numbers swiftly","multiplying accurately", "Stashing data", "Following instructions to the letter ", "Understanding the C language" };

23/4/21

Page 8: 第10章 字符串和字符串函数

1.2 指针和字符串 11.3 p_and_s.c

23/4/21

Page 9: 第10章 字符串和字符串函数

2 字符串输入 如果想把一个字符串读到程序中,必须首先

预留存储字符串的空间,然后使用输入函数来获取这个字符串

23/4/21

Page 10: 第10章 字符串和字符串函数

2.1 创建存储空间 不要指望计算机读的时候会先计算字符串的

长度,然后为字符串分配空间。

23/4/21

Page 11: 第10章 字符串和字符串函数

2.2 gets ()函数 name1.c -- 读取一个名字

23/4/21

Page 12: 第10章 字符串和字符串函数

2.3 fgets () 函数 11.6 name3.c ptr = fgets(name,MAX,stdin);

23/4/21

Page 13: 第10章 字符串和字符串函数

2.4 scanf

11.7 scan_str.c

23/4/21

Page 14: 第10章 字符串和字符串函数

3 字符串输出 puts () fputs () printf ()

23/4/21

Page 15: 第10章 字符串和字符串函数

3.1 puts ()函数 11.18 put_out.c puts ()显示字符串时自动在其后添加一

个换行符。 nono.c -- 不要效仿这个程序

23/4/21

Page 16: 第10章 字符串和字符串函数

3.2 fgets () 函数 fputs(line,stdout);

23/4/21

Page 17: 第10章 字符串和字符串函数

3.3 printf ()函数

23/4/21

Page 18: 第10章 字符串和字符串函数

4 自定义字符串输入 /输出函数 可以自行定义,在 getchar ()和

putchar ()的基础上建立自己的函数 11.10 put1.c 11.11 put2.c

23/4/21

Page 19: 第10章 字符串和字符串函数

5 字符串函数 strlen(), strcar(), strncat(), strcmp(),

strncmp(), strcpy(),strncpy()

23/4/21

Page 20: 第10章 字符串和字符串函数

6 字符串例子 : 字符串排序 11.25 sort_str.c

23/4/21

Page 21: 第10章 字符串和字符串函数

6.1 排序指针而不是字符串 程序的技巧部分在于它并不是重新安排字符

串本身 , 而仅仅重新安排指向字符串的指针

23/4/21

Page 22: 第10章 字符串和字符串函数

6.2 选择排序算法 使用了选择排序 (selection sort) 算法来

进行指针排序 . 其思想是使用一个 for 循环把每个元素轮流与

第一个元素比较 . 如果被比较元素在顺序上先于当前第一个元素 , 程序就交换这二者

23/4/21

Page 23: 第10章 字符串和字符串函数

7 ctype.h 字符函数和字符串 11.26 mod_str.c

23/4/21

Page 24: 第10章 字符串和字符串函数

8 命令行参数 命令行 (command line) 是一一个命令行

环境下 , 用户输入的用于运行程序的行 11.27 repeat.c

23/4/21

Page 25: 第10章 字符串和字符串函数

9 把字符串转换为数字 11.28 hello.c 11.29 strcnvt.c

itoa() 和 ftoa() strtol

23/4/21

Page 26: 第10章 字符串和字符串函数

10 关键概念

23/4/21

Page 27: 第10章 字符串和字符串函数

11 总结

23/4/21