【发布时间】:2014-08-07 02:20:14
【问题描述】:
我正在尝试使用c 中的递归来反转一个单词,我做到了。
#include<stdio.h>
void reverse(char *a){
if(*a){
reverse(a+1);
printf("%c",*a);
}
}
int main() {
char a[] = "i like this program very much";
reverse(a); // i ekil siht margorp yrev
return 0;
}
假设输入字符串是i like this program very much。该函数应将字符串更改为much very program this like i
Algorithm:
1) Reverse the individual words, we get the below string.
"i ekil siht margorp yrev hcum"
2) Reverse the whole string from start to end and you get the desired output.
"much very program this like i"
我已成功完成第 1 步,但我不确定如何继续。请帮忙。
【问题讨论】:
-
您已经“完成”了第 2 步,而不是第 1 步。但重要的第一步是将反转的字符串存储在内存中,而不是打印出来。
-
@sharth 可以就地完成
-
@icepack:我同意。目前,输出到标准输出。如果原始发布者学会了如何将结果存储到程序可访问的任何块内存中,这可能会有所帮助。
-
@Adrian 它不是重复的。