成都创新互联网站制作重庆分公司

c语言交换两个字符串函数 C语言交换两个字符串

c语言怎么把两个字符串交换位置

利用strcpy()函数。

娄烦网站建设公司创新互联,娄烦网站设计制作,有大型网站制作公司丰富经验。已为娄烦上1000家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的娄烦做网站的公司定做!

char a[10] = "abed", b[10] = "efg", t[10];

strcpy(t, a);//a复制给t

strcpy(a, b);//b复制给a

strcpy(b, t);//t复制给b

函数功能是字符串复制,将第一个参数指定的字符串复制到第二个参数指定的位置

两个参数都是字符串首地址。

使用strcpy需要 #includestring.h

C语言中怎样交换两个字符串

不同的情况做法是不同的。

1. 如果是字符数组,char a[50]="String A"; char b[50]="String B";  则

#includestdio.h

void strexchg(char *a, char *b){

char c;

while(*a  *b){

c= *a; *a = *b; *b = c;

a++; b++;

}

c= *a; *a = *b; *b = c;

if(*a)

do *++a = *++b; while(*b);

else if(*b)

do *++b = *++a; while(*a);

}

int main(){

char a[50]="String A"; char b[50]="String B";

printf("Before Exchange :\n\tString A is \"%s\"\n\tString B is \"%s\"\n",a,b);

strexchg(a,b);

printf("After Exchange :\n\tString A is \"%s\"\n\tString B is \"%s\"\n",a,b);

return 0;

}

2 如果两个都是字符指针变量,char *a="String A"; char *b="String B"; 则

#includestdio.h

void strexchg(char **a, char **b){

char *c;

c=*a; 

*a=*b;

*b=c;

}

int main(){

char *a="String A"; char *b="String B"; 

printf("Before Exchange :\n\tString A is \"%s\"\n\tString B is \"%s\"\n",a,b);

strexchg(a,b);

printf("After Exchange :\n\tString A is \"%s\"\n\tString B is \"%s\"\n",a,b);

return 0;

}

C语言中怎样交换两个字符串?

它与简单变量的交换方法相同,但是字符串的传递是通过系统函数实现的。例如: \x0d\x0achar str1[20]={"beijing"},str2[20]={"qindao"}, temp[20]; \x0d\x0astrcpy(str1,temp); strcpy(str2,str1); strcpy(temp,str2); \x0d\x0astrcpy 函数功能是字符串复制,将第一个参数指定的字符串复制到第二个参数指定的位置 \x0d\x0a两个参数都是字符串首地址。 \x0d\x0a使用strcpy需要 #include \x0d\x0a希望能帮助你!


本文标题:c语言交换两个字符串函数 C语言交换两个字符串
URL网址:http://cxhlcq.com/article/hpgigi.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部