#include<stdio.h>
#include<string.h> //字符串引用函数声明
int main(){
char title[]="prison break";
char hero[100]="micheal scofield";
char prisonname[100];
char response[100];
printf("what's the name of the prinson in %s",title);
scanf("%s",prisonname); //数组不需要用取地址符;数组就是地址;
if(strcmp(prisonname,"fox-river")==0) //字符串比较函数
printf("yeah!do you love %s\n",hero);
else{
strcpy(response,"it seems you haven't watched it!\n"); //字符串拷贝函数
printf("%s",response);
}
return 0;
}
更有意思的一点是:
title[0]='t';
title[3]=0; \\相当于结尾的'0\',因此这个后面的数字全被砍掉了;
printf("%s\n",title);
输出的结果只是:tri
what's the name of the prinson in prison break
big boy
it seems you haven't watched it!
tri
Program ended with exit code: 0
字符串拷贝
strcpy(char dest [],char src [] ); //拷贝src到dest
字符串比较大小
int strcmp(char s1 [],char s2 []); //返回0则相等
求字符串长度
int strlen(char s[]); //不算结尾的’\0’
字符串拼接
strcat(char s1[],char s2[]); //s2拼接到s1后面
字符串转成大写
strupr(char s[]);
字符串转成小写
strlwr(char s[]);
char s[100]=“test”;
for( int i = 0; i < strlen(s); ++ i ) {
s[i] = s[i]+1;
}
strlen函数的执行是需要时间的,且时间和字符串的长度成正比 每次循环,都调用strlen函数,这是效率上的很大浪费
应取出s的长度存放在一个变量里面,然后在循环的时候使用该变量
char s[100] = “test”;
int len = strlen(s);
for( int i = 0; i < len; ++ i ) {
}