文档详情

C语言,第8章 数组(字符数组).pptx

发布:2023-10-02约2.05千字共43页下载文档
文本预览下载声明
第8章 数组 (字符数组);内 容 提 要 什么是字符串? 字符数组的作用 字符数组的定义 字符数组的输入和输出 字符数组的初始化 程序实例 字符串复制函数strcpy 字符串连接函数strcat 字符串比较函数strcmp 字符串长度函数strlen 程序实例;什么是字符串?;字符数组;给字符数组赋值;给字符数组赋值;给字符数组赋值;给字符数组赋值;给字符数组赋值;给字符数组赋值;给字符数组赋值;gets函数和scanf输入字符串的区别;字符数组的输出;字符数组的输出;字符数组的输出;字符数组的输出;字符数组程序举例;#include stdio.h int main(void) { char str[128]; int i, zimu = 0, shuzi = 0, kongge = 0, qita = 0; printf(Please input string:); gets(str);;for (i = 0; str[i] != \0; i++) {;printf(“zimu=%d,shuzi=%d,kongge=%d,qita=%d\n, zimu, shuzi, kongge, qita); return 0; };使用字符串处理函数必须加头文件 string.h;字符串复制函数strcpy;字符串连接函数strcat;字符串的比较规则: 对两个字符串从左至右逐个对应字符相比(按ASCII码值大小比较),直到出现不同的字符或遇到‘\0’为止。 ?如全部字符相同,则认为相等; ?若出现不相同的字符,则以第一个不相同的字符的比较结果为准。 例如:“these” 与 “that” “abc” 与 “abc” “36+54” 与 “36”;3. strcmp(字符串1,字符串2);字符串长度函数strlen;程序举例;gets(str1); gets(str2); gets(str3); len1 = strlen(str1); len2 = strlen(str2); len3 = strlen(str3); if (len1 len2) { max = len1; } else { max = len2; } if (len3 max) { max = len3; };程序举例;gets(str1); gets(str2); gets(str3); if (strcmp(str1, str2) 0) { strcpy(maxstr, str1); } else { strcpy(maxstr, str2); } if (strcmp(str3, maxstr) 0) { strcpy(maxstr, str3); };程序举例;char str[5][81], tmp[81]; int i, j; for (i=0; i=4; i++) { gets(str[i]); };for (i=0; i=3; i++) { for (j=i+1; j=4; j++) { if (strcmp(str[i], str[j]) 0) { strcpy(tmp, str[j]); strcpy(str[j], str[i]); strcpy(str[i], tmp); } } };for (i=0; i=4; i++) { printf(“%s\n”, str[i]); };程序举例;程序举例;for (i = 0; str1[i] != \0; i++) { if ((str1[i] = a) (str1[i] = z)) { str2[i] = (a + z) - str1[i]; } else if ((str1[i] = A) (str1[i] = Z)) { str2[i] = (A + Z) - str1[i]; } else { str2[i] = str1[i]; } } str2[i] = \0;;程序举例;for (i=0; str[i]!=\0; i++) { if (str[i] != ) { num++; } else { if (num maxlen) { maxlen = num; } num = 0; } } if (num maxlen) { maxlen = num; };程序举例;int ch, i; char string[101]; for (i = 0; i100; i++) { ch = getchar(); if ((char)ch == ‘\n’ || ch == EOF) { break; } else { string[i] = (char)ch; } } string[i] = \0;;程序举例;for (k=0; k=i-2; k++)
显示全部
相似文档