首页 > 代码库 > c语言--字符串
c语言--字符串
1.字符串
表现形式:char ch[5]={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};
注意点:
双引号括起来
每个字符占用一个字节
以‘\0‘表示字符串结束
int main(int argc, const char * argv[]){ char str[100]="hahahaha\0wewewe\0helloworld"; printf("%s\n",str); printf("%s\n",&str[9]); printf("%s\n",&str[16]); return 0;}
1.1字符串长度
注意点:
字符串有效长度不包含‘\0‘
sizeof 是一个运算符, 求变量或者常量占用内存空间大小
strlen是一个函数, 求字符串有效字符个数
int mystrlen(const char * src){ int i=0; while (src[i]!=‘\0‘) { i++; } return i;}int main(int argc, const char *argv[]){ char str[100]="hello world"; printf("%d \n", mystrlen(str));//调用函数求出长度 printf("%ld \n", sizeof(str)); printf("%ld \n", sizeof("hello world")); printf("str == %ld \n", strlen(str)); return 0;}
1.2字符串拷贝(复制)
函数:
strcpy:
char *strcpy(char *, const char *);
strncpy:
char *strncpy(char *, const char *, size_t);
注意点:
s1的内存空间足够容纳s2字符串
strncpy 选择拷贝的字节数, 不一定包含‘\0‘;
//自定义拷贝函数char *mystrcpy(char *dst, const char *src){ int i =0; while (src[i]) { dst[i]=src[i]; i++; } dst[i]=‘\0‘; return dst;}int main(int argc, const char *argv[]){ char s1[100]; char s2[50]="hello world\n"; //char *str = strcpy(s1, s2); //printf("%s\n",strcpy(s1, s2)); //printf("%s\n",str); //printf("%s\n",mystrcpy(s1, s2)); strncpy(s1, s2, 10); s1[10]=‘\0‘; printf("%s\n", s1); return 0;}
1.3字符串拼接
函数:
strcat:
char *strcat(char *, const char *);
strncat:
char *strncat(char *, const char *, size_t);
strncat在拼接后的新字符串后会添加一个‘\0‘;
//自定义拼接函数char *mystrcat(char *s1, const char *s2){ int i=0,j; while (s1[i]) { i++; } //循环拼接操作 for (j=0; s2[j]!=‘\0‘; j++) { s1[i+j]=s2[j]; } s1[i+j]=‘\0‘; return s1;}int main(int argc, const char *argv[]){ char s1[100]="hello world"; char s2[50]="wearefriend"; //printf("%s\n",strcat(s1, s2)); //printf("%s\n",mystrcat(s1, s2)); printf("%s\n",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]!=‘\0‘)&&(s2[i]!=‘\0‘) ; 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\n", mystrcmp(str1, str2)); printf("%d\n", 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\n", pstr); //printf("%s\n", 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\n",&str1[strlen(str1)]);// char *pstr = strstr(str1, "world");// printf("%s\n", pstr); char *p = str1; while ((p=strstr(p, "world"))) { if (p) { printf("%s\n", 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\n",p);// while ((p=strtok(NULL, ": /^.()-"))) {// printf("%s\n",p);// } char *p = str; while ((p=strtok(p, ": /^.()-"))) { printf("%s\n",p); p=NULL; }// p = strtok(NULL, ":");// printf("%s\n",p); return 0;}
eg.按照空格来分割字符串得到单词数量(不使用系统函数)
int main(int argc, const char *argv[]){ char str[200]; scanf("%[^\n]",str); int i=0; int flag=1;//标志位 int cnt=0;//统计数量 while (str[i]!=‘\0‘) { if (str[i]==‘ ‘) { if (!flag) { flag = 1; } } else { if (flag) { cnt++; flag =0; } } i++; } printf("%d\n", cnt); return 0;}
c语言--字符串