c语言应该知道的字符串函数

网站UI设计

  1.追加字符串 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[100]="hello00or";//被追加提供足够多的容量 char ch2[]=" woddrwe";//要追加的数据 strcat(ch1,ch2);//追加字符串 printf("%s\n",ch1); system("pause"); return 0; } (1)自己写一个strcat #include<stdio.h> #include <time.h> #include <Windows.h> void my_strcat(char* ch1,char* ch2) { while(*ch1)*ch1++; while(*ch1++=*ch2++); } int main() { char ch1[100]="hello00or"; char ch2[]=" woddrwe"; my_strcat(ch1,ch2); printf("%s\n",ch1); system("pause"); return 0; } 2查找字符串 #include<stdio.h> #include <time.h> #include <Windows.h> char* my_find(char* src,char* dest)//传入源数据,查找数据 { char* fsrc=src;//源数据要遍历的指针 char* rsc=src;//记录相同数据的指针 char* tdest=dest;//要查找数据指针 while (*fsrc) { rsc=fsrc;//记录初始数据 while (*fsrc==*tdest&&tdest!=0)//查找数据不为\0 { fsrc++;//遍历源数据 tdest++;//遍历记录数据 } if (*tdest==0)//遍历所有要查找的数据 { return rsc;//返回查找的数据 } tdest=dest;//没有找到相同数据,要查找数据重新到第一个位置 fsrc=rsc;//源数据要遍历的位置回到记录的数据处 fsrc++; } return NULL; } int main() { char ch1[100]="hello00or"; char ch2[]="re"; char* p=my_find(ch1,ch2); printf("%s\n",p); system("pause"); return 0; } 3.比较字符串 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[100]="hello00or"; char ch2[]="re"; int value=strcmp(ch1,ch2);//比较字符串是否相等返回int型0为相同,其它不同 strcmp(ch1,ch2,n)//比较前n个字符 printf("%d\n",value); system("pause"); return 0; } (2)定义自己的比较字符串(程序有点问题) #include<stdio.h> #include <time.h> #include <Windows.h> int my_strcmp(char* ch1,char*ch2) { while(*ch1==*ch2) { if(!*ch1) return 0; ch1++; ch2++; } return *ch1>*ch2 ? 1:-1; } int main() { char ch1[100]="hell\0o00or"; char ch2[]="he"; int value=my_strcmp(ch1,ch2); printf("%d\n",value); system("pause"); return 0; } 4.sprintf写入字符串 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[100]; sprintf(ch1,"hello world");//格式化操作 printf("%s\n",ch1); system("pause"); return 0; } 5.sscanf(读取字符串) #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[]="hello world"; char ch2[20]; sscanf(ch1,"%[^\n]",ch2);//格式化操作 printf("%s\n",ch2); system("pause"); return 0; } 6strchr//查找字符出现的位置 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[]="hello world"; char ch2='l'; char* value=strchr(ch1,ch2); printf("%s\n",value); system("pause"); return 0; } (2)自己写一个算法查找字符 #include<stdio.h> #include <time.h> #include <Windows.h> char* my_strchr(char* s,char c) { while(*s) { if(*s==c) return s; s++; } return NULL; } int main() { char ch1[]="hello world"; char ch2='l'; char* value=my_strchr(ch1,ch2); printf("%s\n",value); system("pause"); return 0; } 7.strstr字符串中查找字符串的位置 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[]="hello world"; char ch2[]="llo"; char* value=strstr(ch1,ch2); printf("%s\n",value); system("pause"); return 0; } 8.字符串分割 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[]="www.htmll.c.om"; char* p=strtok(ch1,".");//字符串分割 p=strtok(NULL,".");//指针指向空,第二次分割 p=strtok(NULL,"."); printf("%s\n",p); system("pause"); return 0; } (2)字符串多次分割 #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[]="www.htmll.c.om"; char* p=strtok(ch1,".");//字符串分割 while(p) { printf("%s\n",p); p=strtok(NULL,"."); } system("pause"); return 0; } 9.字符串atoi转换成整型(atof,atol用法相似) #include<stdio.h> #include <time.h> #include <Windows.h> int main() { char ch1[]="1233"; int p=atoi(ch1);//字符串转化为整形 //可以以空格开始识别sscanf用法相似 printf("%d\n",p); system("pause"); return 0; }

标签: 网站UI设计