gpt4 book ai didi

c - 如何获得返回数组而不是打印数组的函数?

转载 作者:行者123 更新时间:2023-11-30 18:10:24 25 4
gpt4 key购买 nike

该函数写在下面。我希望此函数返回排列后的字符串的数组而不是打印它,因为我的目标是能够查看给定字符串可能有多少排列,并且要求使用递归。

void permuteString(char *a, int first, int last) //parameters are array, first index, and final index //requires a swap function
{
int i;

if (first == last) //this is the base case that indicates that there are no more letters to be permuted
{
printf("%s\n", a);
}

else
{
for(i = first; i <= last; i++)
{
swap((a + first), (a + i));
permuteString(a, first + 1, last);
swap((a + first), (a + i));
}
}
}

最佳答案

我了解您需要返回字符数组而不是打印字符数组,因此,请首先更改函数的返回类型并按如下所示返回字符数组,

        Char *permuteString(char *a, int first, int last) //parameters are array, first index, and final index //requires a swap function
{
int i;

if (first == last) //this is the base case that indicates that there are no more letters to be permuted
{
// printf("%s\n", a);
return a;
}

else
{
for(i = first; i <= last; i++)
{
swap((a + first), (a + i));
permuteString(a, first + 1, last);
swap((a + first), (a + i));
}
}
}

关于c - 如何获得返回数组而不是打印数组的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58304958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com