程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> c語言中字符串函數的使用

c語言中字符串函數的使用

編輯:關於C
#include
#include
/*
char s1[]="I am a student";
char s2[20]="teacher";
char s3[]="student";
int result;
char s4[20],*p;
1.串的長度
int strlen(char *str):
printf("%d\n",strlen(s1));//長度為14
printf("%d\n",strlen(s2));//長度為7



2.復制
char *strcpy(char *str1,char *str2):
strcpy(s4,s2);//把s2復制給s4
printf("%s\n",s4);//輸出teacher




3.比較
int strcmp(char *str1,char *str2):
result=strcmp(s2,s3);
printf("%d\n",result);//s2>s3




4.字符串定位
char *strchr(char *str,char ch);
p=strchr(s1,'s');//p指向在s1中字符's'的位置
printf("%s\n",p);//輸出student




5.子串查找
char *strstr(char *s1,char *s2);
p=strstr(s1,s3);//p指向在s1中字符's'的位置
printf("%s\n",p);//輸出student




6.連接
char * strcat(char *str1,char *str2):
strcat(s2,s3);
printf("%s\n",s2);//輸出teacherstudent


*/
void ReverseName(char *name,char *newName){


char *p;
p=strchr(name,' ');//字符定位
*p='\0';
printf("%s\n",name);
printf("%s\n",p);
strcpy(newName,p+1);//復制
printf("--%s\n",newName);
strcat(newName,",");//連接
strcat(newName,name);//連接
*p=' ';
printf("%s\n",name);
}




int main(){


char name[]="jie wang",newName[30];
ReverseName(name,newName);
printf("hello world\n");
return 0;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved