1.字符串
表现形式:char ch[5]={'a','b','c','d','e'};
注意点:
双引号括起来
每个字符占用一个字节
以' '表示字符串结束
int main(int argc, const char * argv[]) { char str[100]="hahahaha wewewe helloworld"; printf("%s ",str); printf("%s ",&str[9]); printf("%s ",&str[16]); return 0; }
1.1字符串长度
注意点:
字符串有效长度不包含' '
sizeof 是一个运算符, 求变量或者常量占用内存空间大小
strlen是一个函数, 求字符串有效字符个数
int mystrlen(const char * src) { int i=0; while (src[i]!=' ') { i++; } return i; } int main(int argc, const char *argv[]) { char str[100]="hello world"; printf("%d ", mystrlen(str));//调用函数求出长度 printf("%ld ", sizeof(str)); printf("%ld ", sizeof("hello world")); printf("str == %ld ", strlen(str)); return 0; }
1.2字符串拷贝(复制)
函数:
strcpy:
char *strcpy(char *, const char *);
strncpy:
char *strncpy(char *, const char *, size_t);
注意点:
s1的内存空间足够容纳s2字符串
strncpy 选择拷贝的字节数, 不一定包含' ';
//自定义拷贝函数 char *mystrcpy(char *dst, const char *src) { int i =0; while (src[i]) { dst[i]=src[i]; i++; } dst[i]=' '; return dst; } int main(int argc, const char *argv[]) { char s1[100]; char s2[50]="hello world "; //char *str = strcpy(s1, s2); //printf("%s ",strcpy(s1, s2)); //printf("%s ",str); //printf("%s ",mystrcpy(s1, s2)); strncpy(s1, s2, 10); s1[10]=' '; printf("%s ", s1); return 0; }
1.3字符串拼接
函数:
strcat:
char *strcat(char *, const char *);
strncat:
char *strncat(char *, const char *, size_t);
strncat在拼接后的新字符串后会添加一个' ';
//自定义拼接函数 char *mystrcat(char *s1, const char *s2) { int i=0,j; while (s1[i]) { i++; } //循环拼接操作 for (j=0; s2[j]!=' '; j++) { s1[i+j]=s2[j]; } s1[i+j]=' '; return s1; } int main(int argc, const char *argv[]) { char s1[100]="hello world"; char s2[50]="wearefriend"; //printf("%s ",strcat(s1, s2)); //printf("%s ",mystrcat(s1, s2)); printf("%s ",strncat(s1, s2, 5)); return 0; }
1.4字符串比较函数
函数:
strcmp:
int strcmp(const char *, const char *);
strncmp:
int strncmp(const char *, const char *, size_t);
s1 > s2 返回一个正数
s1 == s2 返回0
s1 < s2 返回一个负数
//自定义比较函数 int mystrcmp(const char* s1, const char *s2) { int i; //循环判断到字符串结尾处 for (i=0; (s1[i]!=' ')&&(s2[i]!=' ') ; i++) { if (s1[i]==s2[i]) { continue; } else { break; //return s1[i]-s2[i]; } } //返回一个比较数 return s1[i]-s2[i]; } int main(int argc, const char *argv[]) { char *str1 = "borld"; char *str2 = "zorld"; //printf("%d ", mystrcmp(str1, str2)); printf("%d ", strncmp(str1, str2, 7)); //char s1[30]="hello world"; return 0; }
1.5字符串查找
函数:
strchr:
char *strchr(const char *, int);
strrchr:
char *strrchr(const char *, int);
char *mystrchr(const char *s1, char ch) { int i=0; while (s1[i]) { if (s1[i]==ch) { break; } i++; } return (char *)&s1[i]; } int main(int argc, const char *argv[]) { char str[100]="hello world!"; char *pstr = mystrchr(str, 'l'); printf("%s ", pstr); //printf("%s ", pstr); return 0; }
1.6查找字符串
函数:
strstr:
char *strstr(const char *, const char *);
int main(int argc, const char *argv[]) { char str1[100]="helloworld helloworld"; printf("%p ",&str1[strlen(str1)]); // char *pstr = strstr(str1, "world"); // printf("%s ", pstr); char *p = str1; while ((p=strstr(p, "world"))) { if (p) { printf("%s ", p); p+=sizeof("world")-1; } } return 0; }
1.7字符串分割
函数:
strtok:
char *strtok(char *, const char *);
int main(int argc, const char *argv[]) { char str[100]="hello world:good-bye:://we^are..friend()ok///"; // char *p=strtok(str, ": "); // printf("%s ",p); // while ((p=strtok(NULL, ": /^.()-"))) { // printf("%s ",p); // } char *p = str; while ((p=strtok(p, ": /^.()-"))) { printf("%s ",p); p=NULL; } // p = strtok(NULL, ":"); // printf("%s ",p); return 0; }
eg.按照空格来分割字符串得到单词数量(不使用系统函数)
int main(int argc, const char *argv[]) { char str[200]; scanf("%[^ ]",str); int i=0; int flag=1;//标志位 int cnt=0;//统计数量 while (str[i]!=' ') { if (str[i]==' ') { if (!flag) { flag = 1; } } else { if (flag) { cnt++; flag =0; } } i++; } printf("%d ", cnt); return 0; }