1.将数组char a[]=“acegikm”; char b[]=“bdfhjlnopq”; 将上面两个数组按abcd…整合到数组c中。用指针实现。
#include <stdio.h>
#include <string.h>
int main() {
char s1[32] = {0};
char s2[32] = {0};
char s3[32] = {0};
printf("请输入第一个字符串\n");
gets(s1);
printf("请输入第二个字符串\n");
gets(s2);
char *p = s1,*q = s2,*k = s3;
while (*p != '\0' && *q != '\0'){
if(*p < *q){
*k = *p;
p++;
} else{
*k = *q;
q++;
}
k++;
}
if(*p == '\0'){
while(*q){
*k = *q;
q++;
k++;
}
}
if(*q == '\0'){
while(*p){
*k = *p;
p++;
k++;
}
}
puts(s3);
return 0;
}
2.从终端输入字符串,如将字符串”I love china“,将其改变为”china love i“。(不借助于中间变量)
#include<stdio.h>
#include<string.h>
#define N 32
int swap(char *p, char *q);
int main() {
char s[N] = {0};
gets(s);
char *p = s;
char *q = s + strlen(s) -1;
swap(p, q);
while (*p) {
if(p == 32){
q++;
}
q = p;
while (32 != *q && '\0' != *q) {
q++;
}
swap(p, q - 1);
p = q;
}
puts(s);
return 0;
}
int swap(char *p, char *q) {
while (p < q) {
*p ^= *q;
*q ^= *p;
*p++ ^= *q--;
}
}