码迷,mamicode.com
首页 > 编程语言 > 详细

c语言中字符串函数的使用

时间:2017-05-31 10:16:59      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:子串   strstr   查找   长度   content   str1   res   strlen   wan   

#include<stdio.h>
#include<string.h>
/*
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;
}

c语言中字符串函数的使用

标签:子串   strstr   查找   长度   content   str1   res   strlen   wan   

原文地址:http://www.cnblogs.com/zsychanpin/p/6922485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!