※ 引述《sagwow (不是我)》之铭言:
: 问题(Question):
: 字串互换
: 喂入的资料(Input): apple dog
: 预期的正确结果(Expected Output):dog apple
: 补充说明(Supplement):
: 我宣告char a[100][1000],想交换其中a[0]和a[1]两个字串,
: 想请问有没有不用一个字符一个字符交换的方法。
GitHub Gist: https://gist.github.com/anonymous/ea41f753a335f34a3ec458f730c8f499
不一定要用二维阵列去处理,有 in-place 的 recursive 算法提供参考
大致上的做法就是 先对原字串反转一次,再对子字串反转一次就可以了
#include <stdio.h>
#include <string.h>
char *swapstr_rec(char *str, int begin, int end, int first)
{
while (begin < end - 1)
{
char tmp = str[begin];
str[begin++] = str[