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

  • 格式:pdf
  • 大小:1.13 MB
  • 文档页数:76

综合实例:定义字符串
// initializing a pointer const char * m3 = "\nEnough about me -- what's your name?"; // initializing an array of string pointers const char * mytal[LIM] = { // array of 5 pointers "Adding numbers swiftly", "Multiplying accurately", "Stashing data", "Following instructions to the letter", "Understanding the C language" }; ……
常用的字符串处理函数
1、字符串的输入和输出
输入字符串:
gets( )或fgets()或scanf( )
输出字符串:
puts( ) 或fputs()或printf( )
函数原型定义在stdio.h
字符串的输入
char str[80]; i = 0; while((str[i] = getchar( )) != '\n') i++; str[i] = '\0';
字符串的输入 (P292)
字符串的输入
/* scan_str.c -- using scanf() */ #include <stdio.h> int main(void) { char name1[11], name2[11]; int count; printf("Please enter 2 names.\n"); count = scanf("%5s %10s",name1, name2); printf("I read the %d names %s and %s.\n", count, name1, name2); return 0; }
字符串的访问和输入/输出
【例】使用函数gets(),从键盘输入一个带有空 格的人名,然后把它显示在屏幕上
不限制输入字符串的长度 易引起缓冲区溢出,给黑客攻击以可乘之机
字符串的访问和输入/输出
【例】使用函数gets(),从键盘输入一个带有空 格的人名,然后把它显示在屏幕上
能够限制输入字符串的长度
字符串的访问和输入/输出
【例】从键盘输入一个带有空格的人名,然后在显 示人名的前面显示"Hello", I said to
字符串的访问和输入/输出
【例】从键盘输入一个带有空格的人名,然后在显 示人名的前面显示"Hello", I said to
字符串的输入
(3) scanf("%s", str)
输入参数:字符数组名,不加地址符 从第一个非空白字符开始,遇下一个空白 字符时输入结束,并自动将输入的一串 字符和 ‘\0’ 送入数组中
字符数组与字符指针的重要区别
char sa[ ] = "This is a string"; char * sp = "This is a string";
sa sp Thi s i s a s t r i n g \0
Thi s
i s
a
s t r i n g \0
• 如果要改变数组sa所代表的字符串,只能改变 数组元素的内容 • 如果要改变指针sp所代表的字符串,通常直接 改变指针的值,让它指向新的字符串
字符指针-先赋值,后引用
定义字符指针后,如果没有对它赋值,指针的值不 确定。 char * s ; 不要引用未赋值的指针 scanf(“%s”, s); char * s, str[20]; s = str; scanf(“%s”, s); 定义指针时,先将它的初值置为空 char * s = NULL ;
字符串的输入
(1) gets(str)
遇回车输入结束,自动将输入的一串字符 和 ‘\0’ 送入数组中
gets()不检查预留存储区是否能容纳实际输入的数 据,多出来的字符简单地溢出到相邻的内存 出错或遇到EOF,返回空(或0)地址,此空地 址被称为空指针,在stdio.h中用常量NULL表示
例:输入一行字符,统计其中的英文字符、数字字 符、空格和其他字符的个数。
回顾上次课内容
字符串常量 字符数组、字符指针 字符指针变量与字符数组的区别 字符指针-先赋值,后引用
回顾:下面能正确进行字符串赋值操作的是_____。 A)char s[5]={“ABCDE”}; B)char s[5]={‘A’,’B’,’C’,’D’,’E’}; C)char *s; s=“ABCDE”; √ D)char *s; scanf(“%s”,&s);
综合实例:定义字符串
int main(void) { char name[LINELEN]; char talents[LINELEN]; int i; // initializing a dimensioned char array const char m1[40] = "Limit yourself to one line's worth."; // letting the compiler compute the array size const char m2[] = "If you can't think of anything, fake it.";
字符串和字符指针
char sa[ ] = "array"; char * sp = "point"; printf("%s ", sa); printf("%s ", sp); printf("%s\n", "string"); array point string 数组名sa、指针sp和字符串 "string" 的值都是地址 ray nt tring printf("%s ", sa+2); printf("%s ", sp+3); printf("%s\n", "string"+1);
字符串常量 "array" "point " “I am handsome. "
用一对双引号括起来的字符序列 被看做一个特殊的一维字符数组,在内存中连续存
放,以空字符结束’\0’
实质上是一个指向该字符串首字符的指针常量
char sa[ ] = “array”; //字符串常量的存储本质 char * sp = “point”; //字符串常量本身是个指针
字符串、字符数组、字符指针
字符串
一串以’\0’结尾的字符在C语言中被看作字符串 用双引号括起的一串字符是字符串常量,C语言自动为其添
加’\0’终结符 C语言并没有为字符串提供任何专门的表示法,完全使用字 符数组和字符指针来处理
字符数组
每个元素都是字符类型的数组
char
string[100];
字符指针变量与字符数组的区别
初始化含义不同
1、char * pstr = ”china”; 等价于 char * pstr; pstr = ”china”; 2、char str[6] = ”china”; 不等价于 char str[6]; str[] = ”china”; ห้องสมุดไป่ตู้符指针是变量,而数组名是地址常量
练习:设有如下的程序段: char str[]=“Hello”; char *ptr; ptr=str; 执行完上面的程序段后,*(ptr+5)的值是_____。 B)‘\0’ √ C)不确定的值 D)‘o’的地址 A)‘o’
练习:以下程序的输出结果是_____。 #include <stdio.h> char cchar(char ch) { if (ch>=‘A’&&ch<=‘Z’) ch=ch-’A’+’a’; return ch; } main() { char s[]=“ABC+abc=defDEF”,*p=s; while (*p) { *p=cchar(*p); p++; } printf(“%s\n”,s); } A)abc+ABC=DEFdef B)abc+abc=defdef C)abcABCDEFdef D)abcabcdefdef
在定义一个数组时,在编译时即分配单元,有确定
地址,而定义一个字符指针变量时,如未对它赋初 值,则其所指数据是不定的,因而使用是危险的。 例如,输入字符串时 char str[10]; scanf("%s", str); /*正确*/ char * a; scanf("%s", a); /*错误*/ 应为: char * a; char str[10]; a = str; scanf("%s", a); /*正确*/
#include<string.h> #include<stdio.h> int main(void) { FILE*stream; char msg[100]; /*open a file */ stream=fopen("aa.txt","r+"); fgets(msg,100,stream); /*display the string*/ printf("%s",msg); fclose(stream); return 0; }