字符串处理
怎样可以简单的将两个字符串交换??如:a[5]=qwert;b[5]=dcdfd;
交换后a[5]=dcdfd;b[5]=qwert;
请指教!
#include <stdio.h> void swap(char * p1, char * p2) { if(p1 == p2) return; *p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2; } int main(void) { char a[] = "qwert", b[] = "dcdfd"; char * p1 = a, * p2 = b; printf("Before swap : \na : %s\nb : %s\n", a, b); while(*p1 && *p2) swap(p1++, p2++); printf("After swap : \na : %s\nb : %s\n", a, b); return 0; }
#include <stdio.h> int main() { char a[5]= "qwert"; char b[5]= "dcdfd"; char c[5]; int i; for(i = 0;i < 5;i ++) { c[i] = a[i]; a[i] = b[i]; b[i] = c[i]; } for(i = 0;i < 5;i ++) { printf("%c",a[i]); } printf("\n\n"); for(i = 0;i < 5;i ++) { printf("%c",b[i]); } return 0; }