字符数组:char word[] = {'H','e','l','l','o','!'};
字符串:char word[] = {'H','e','l','l','o','!','\0'};
char line[10] = "Hello";
Example 01:
#include <stdio.h>
int main(){
char *s = "Hello World\0";
char s1[] = "Hello World\0";
s1[0] = 'B';
printf("s=%s\n",s);
printf("s1=%s\n",s1);
printf("Here!s1[0]=%c\n",s1[0]);
return 0;
}
char string[8];
scanf("%s",string);
printf("%s",string);
字符串数组
putchar
getchar
案例:
#include <stdio.h>
int main(){
int ch;
while((ch = getchar()) != EOF){
putchar(ch);
}
printf("EOF\n");
return 0;
}
在使用字符串处理函数时,应当在程序文件的开头用 #include
把 ”string.h“ 文件包含到本文件中, string.h标准库中包含函数:
strlen
测字符串长度的函数
size_t strlen(const char *s);
返回s的字符串长度(不包括结尾的0)
Example 01:
#include <stdio.h>
#include <string.h>
int main(){
char line[] = "Hello";
printf("strlen=%lu\n",strlen(line));
printf("sizeof=%lu\n",sizeof(line));
return 0;
}
#include <stdio.h>
#include <string.h>
int mylen(const char *s){
int idx = 0;
while(s[idx]!='\0'){
idx++;
}
return idx;
}
int main(){
char line[] = "Hello";
printf("strlen=%lu\n",mylen(line));
printf("sizeof=%lu\n",sizeof(line));
return 0;
}
strcmp
字符串比较函数
int strcmp(const char s1,const char s2);
-1:s1 < s2
Example 01:
#include <stdio.h>
#include <string.h>
int main(){
char s1[] = "abc";
char s2[] = "abc";
printf("%d\n",strcmp(s1,s2));
return 0;
}
#include <stdio.h>
#include <string.h>
int main(){
char s1[] = "abc";
char s2[] = "Abc";
printf("%d\n",strcmp(s1,s2));
printf("%d\n",'a'-'A');
return 0;
}
#include <stdio.h>
#include <string.h>
int mycmp(const char *s1,const char *s2){
int idx = 0;
while(s1[idx] == s2[idx] && s1[idx] != '\0'){
// if(s1[idx] != s2[idx]){
// break;
// }else if(s1[idx] == '\0'){
// break;
// }
idx++;
}
return s1[idx] - s2[idx];
}
int main(){
char s1[] = "abc";
char s2[] = "Abc";
printf("%d\n",mycmp(s1,s2));
printf("%d\n",'a'-'A');
return 0;
}
#include <stdio.h>
#include <string.h>
int mycmp(const char *s1,const char *s2){
while(*s1 == *s2 && *s1 != '\0'){
s1++;
s2++;
}
return *s1 - *s2;
}
int main(){
char s1[] = "abc";
char s2[] = "Abc";
printf("%d\n",mycmp(s1,s2));
printf("%d\n",'a'-'A');
return 0;
}
strcpy
复制一个字符串
//动态申请内存
char *dst = (char*)malloc(strlen(src)+1);
//拷贝src到dst
strcpy(dst,src);
#include <stdio.h>
#include <string.h>
int mycpy(char *dst,char *src){
int idx = 0;
while(src[idx]){
dst[idx] = src[idx];
idx++;
}
//dst[idx] = src[idx];
dst[idx] = '\0';
return dst;
}
int main(){
char s1[] = "abc";
char s2[] = "Abc";
printf("%s\n",strcpy(s1,s2));
return 0;
}
#include <stdio.h>
#include <string.h>
int mycpy(char *dst,char *src){
char *ret = dst;
// while(*src){
//// *dst = *src;
//// dst++;
//// src++;
// *dst++ = *src++;
// }
while(*dst++ = *src++){
}
*dst = '\0';
return ret;
}
int main(){
char s1[] = "abc";
char s2[] = "Abc";
printf("%s\n",strcpy(s1,s2));
return 0;
}
strncpy
strncpy(str1,str2,2);
Example 01:
#include <stdio.h>
#include <string.h>
int mycpy(char *dst,char *src){
int idx = 0;
while(src[idx]){
dst[idx] = src[idx];
idx++;
}
//dst[idx] = src[idx];
dst[idx] = '\0';
return dst;
}
int main(){
char s1[] = "abc";
char s2[] = "ACd";
printf("%s\n",strncpy(s1,s2,2));
return 0;
}
strcat
字符串连接函数
char strcat(char restrict s1, const char *restrict s2);
#include <stdio.h>
int main(){
char str1[30] = {"I am "};
char str2[] = {"a senior."};
puts(strcat(str1,str2));
return 0;
}
注: 用 puts 和 gets 函数只能输出或输入一个字符串,不能写成 puts(str1,str2); gets(str1,str2);
strchr
返回NULL表示没有找到
Example 01:
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "hello";
char *p = strchr(s,'l');
p = strchr(p+1,'l');
printf("%s\n",p);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "hello";
char *p = strchr(s,'l');
char *t = (char*)malloc(strlen(p)+1);
strcpy(t,p);
printf("%s\n",t);
free(t);
return 0;
}
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "hello";
char *p = strchr(s,'l');
char c = *p;
*p = '\0';
char *t = (char*)malloc(strlen(s)+1);
strcpy(t,s);
printf("%s\n",t);
free(t);
return 0;
}
strrchr
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "hello";
char *p = strchr(s,'l');
p = strrchr(p,'l');
printf("%s\n",p);
return 0;
}
strstr
strlwr
#include <stdio.h>
int main(){
char c[]="ABCDefG";
printf("%s\n",strlwr(c));
return 0;
}
strupr
将字符串中小写字母转换为大写字母函数
Example 01:
#include <stdio.h>
int main(){
char c[]="ABCDefG";
printf("%s\n",strupr(c));
return 0;
}
安全问题