标签:
什么是字符串?
字符串常量是放在一堆双引号中的一串字符或符号。
任何一对双引号之间的任何内容都会被编译器视为字符串,包括特殊字符和嵌入的空格。
字符串的结尾时空字符,写作\0.
注意:C中的字符串总是由\0结束,所以字符串的长度永远比字符串中的字符数多1
其他位置的\0不能作为字符串的一部分,第一个\0的位置就是字符串的结尾部分
测试代码
#include <stdio.h> int main(void) { printf("The character \0 is used to terminate a string."); return 0; }
处理字符串和文本的办法
C语言对变量存储字符串的语法没有特殊的规定,而且C根本就没有字符串变量,也没有处理字符串的特殊运算符。
使用char类型的数组保存字符串,这是字符串变量的最简单的形式。
char saying[20];
这个saying变量可以存储一个至多包含19个字符的字符串,因为必须有一个位置给空字符。
也可以这样声明并初始化: char saying=“This is a string." 在这个例子中char数组的长度是18
也可以用一个字符串初始化char类型数组的部分元素,例如:
char str[40]="To be"; 这里str[0]~[4]是指定字符串元素,str[5]是空字符,后面为空。
处理标准信息是使用char数组并const声明为常量。如果声明了char为常量了,后面就不能更改这个数组了。
输出字符串:
#include <stdio.h> int main(void) { const char message[]="The end of the world is nigh"; printf("\nThe message is: %s",message); return 0; }
转换指定符为%s
直接使用数组存储字符串缺点是浪费内存,因为没有指定数组长度,所以数组的默认长度是最大的字符串的长度。所以确定字符串的长度是很重要的。
确定字符串长度demo:
#include <stdio.h> int main(void) { char str1[]="To be or not to be"; char str2[]=",that is the question"; int count1=0, count2=0; while(str1[count1]!=‘\0‘) { count1++; } printf("\nThe length of the string \"%s\" is %d characters.",str1,count1); while(str2[count2]!=‘\0‘) { count2++; } printf("\nThe length of the string \"%s\" is %d characters.",str2,count2); return 0; }
字符串操作
连接字符串(确定第一个字符串是否内存足够存储两个字符串的量,然后将字符串2复制到1的尾部)
#include <stdio.h> int main(void) { char str1[]="To be or not to be"; char str2[]=",that is the question"; int count1=0; int count2=0; while(str1[count1]) { count1++; } while(str2[count2]) { count2++; } int x=sizeof str1; if(x < count1+count2+1 ) /* a char size is 1 */ { printf("\nYou can‘t put a quart into a pint pot."); }else { count2=0; /* while(str2[count2]) { str1[count1++]=str2[count2++]; } */ while(str1[count++]=str2[count++]); str1[count1]=‘\0‘; printf("\n%s\n",str1); } return 0; }
字符串数组
可以使用char类型的二位数组存储字符串,数组的每一行都用来存储一个字符串,这样就可以存入一串字符串。
demo:
char saying[3][32]={ "Manners maketh man." "Many hands make light work." "Too many cooks spoil the broth." };
不需要用括号括起每个字符串;必须指定最后一维的大小。
saying[i][j]第一个索引i指定数组中的行,第二个索引j指定该行中的一个字符。引用一个索引号,在方括号中只包括一个索引符。
demo:
#include <stdio.h> int main(void) { char saying[][32] ={ "Manners maketh man.", "Many hands make light work.", "Too many cooks spoil the broth." }; int length=sizeof saying/sizeof saying[1]; for(int i=0;i<length;i++) { printf("\n%s",saying[i]); } return 0; }
字符串函数
需要导入头文件<string.h>
复制字符串函数strcpy(string1,string2);(字符串替换函数)
参数是char数组名,将string2复制到string1中,取代原来的strig1的内容,也就是一个替换行数这个复制包括终止符号‘\0‘。
要求,string1的长度>=string2的长度。strcpy()函数不检查字符串长度,通常需要我们先进行判断。
确定字符串长度函数strlen().
strlen(字符串数组名);返回的是一个size_t类型的整数
size_t在头文件<stddef.h>定义
连接字符串函数stract()
strcat(str1,str2);将str2连接到str1的末尾,返回连接后的值
strncat(str1,str2,len);将str2的len个字符连接到str1的末尾,返回连接后的值
这两个函数也需要检查str1的内存是否足够进行操作在使用函数之前,否则,这个函数也会与其他字符串赋值函数一样会覆盖目标数组之后的内存空间。
demo:
#include <stdio.h> #include <stddef.h> #include <string.h> #define STR_LENGTH 40 int main(void) { char str1[STR_LENGTH]="To be or not to be"; char str2[STR_LENGTH]=",that is the question"; if(STR_LENGTH>strlen(str1)+strlen(str2)) { printf("\n%s\n",strcat(str1,str2)); } else { printf("\nYou can‘t pu a quare into a pot."); } if(STR_LENGTH>strlen(str1)+strlen(str2)) { printf("\n%s\n",strncat(str1,str2,5)); }else { printf("\nYou can‘t pu a quare into a pot."); } return 0; }
标签:
原文地址:http://www.cnblogs.com/aigeileshei/p/5441057.html