C 为什么这个倒装句算法不起作用?
对于人类可读代码,代码中有一些注释:C 为什么这个倒装句算法不起作用?,c,C,对于人类可读代码,代码中有一些注释: #include <stdio.h> #include <string.h> #define SIZE 100 //size of the input array and output array #define ACCUM_CHAR_SIZE 25 //size of the temp array int main(){ char i[SIZE]; char acc[ACCUM_CHAR_SIZE]; ch
#include <stdio.h>
#include <string.h>
#define SIZE 100 //size of the input array and output array
#define ACCUM_CHAR_SIZE 25 //size of the temp array
int main(){
char i[SIZE];
char acc[ACCUM_CHAR_SIZE];
char o[SIZE];
int it_l = 0, it_a = 0, it_r = 0;
//it_l is the iterator to the input sentence,
//it_a is the iterator to the temp array
//it_r is the iterator to the output sentence
printf("Enter a sentence:");
gets(i);
int len = strlen(i) - 1;
while(it_l <= len){
if(i[len - it_l] != ' '){
acc[it_a] = i[len - it_l]; //add letters to acc until space
it_a++;
}
else{
it_a -= 1;
//acc is reversed, I reversed it again to the output sentence
while(it_a >= 0){
o[it_r] = acc[it_a];
it_r++;
it_a--;
}
it_r += 1;
o[it_r] = 32; //put a space
it_a = 0; //reset the temp array
strcpy(acc, ""); //clear the acc
}
it_l++;
}
printf("%s", o);
}
#包括
#包括
#定义大小100//输入数组和输出数组的大小
#定义ACCUM_CHAR_SIZE 25//临时数组的大小
int main(){
字符i[大小];
char acc[累计字符大小];
字符o[大小];
int it_l=0,it_a=0,it_r=0;
//它是输入句子的迭代器,
//它是临时数组的迭代器
//它是输出句子的迭代器
printf(“输入一个句子:”);
获取(i);
int len=strlen(i)-1;
while(it_l=0){
o[it_r]=acc[it_a];
it_r++;
这是一个--;
}
it_r+=1;
o[it\u r]=32;//放一个空格
it_a=0;//重置临时数组
strcpy(acc,“”;//清除acc
}
it_l++;
}
printf(“%s”,o);
}
该程序理论上看起来不错,但在执行时,它有时会打印垃圾值,只打印一些单词,或只使用垃圾值而不是空格反转了一半的句子
上面的程序是将每个字保存到temp,并将reverse temp(存储字时temp被反转)返回到输出。然而,它失败了
谢谢您的帮助。至少有三个问题 第一个问题是,您永远不会终止字符串
o
来进行更改:
printf("%s", o);
进入
第二个问题是您错误地增加了it\r
。改变
it_r += 1;
o[it_r] = 32; //put a space
进入
第三个问题是您无法处理输入的第一个字(因为前面没有空格)。我把那个问题留给你做练习
顺便说一句:不要使用get
读取输入。改用fgets。\include
#include <stdio.h>
#include <string.h>
#define SIZE 100 //The size of input array is prefered to be equal to ouput array.
int main(){
char input[SIZE];
char output[SIZE];
int i = 0, j = 0;
//i is the iterator to the input sentence,
//j is the iterator to the output sentence
printf("Enter a sentence:");
gets(input);
int len = strlen(input) - 1; //Total length.
j = len;
while(input[i]!= NULL){
output[j] = input[i];
i++;
j--;
}
output[len+1]= NULL;
printf("%s", output);
printf("Finished");
}
#包括
#定义大小100//输入数组的大小最好等于输出数组的大小。
int main(){
字符输入[大小];
字符输出[大小];
int i=0,j=0;
//i是输入句子的迭代器,
//j是输出句子的迭代器
printf(“输入一个句子:”);
获取(输入);
int len=strlen(输入)-1;//总长度。
j=len;
while(输入[i]!=NULL){
输出[j]=输入[i];
i++;
j--;
}
输出[len+1]=NULL;
printf(“%s”,输出);
printf(“成品”);
}
尝试下面给出的修改后的代码,已对更改的部分进行了注释(为便于阅读,已删除所有其他注释)
#包括
#包括
#定义大小100
#定义累计字符大小25
int main(){
字符i[大小];
char acc[累计字符大小];
字符o[大小];
int it_l=0,it_a=0,it_r=0;
printf(“输入一个句子:”);
获取(i);
int len=strlen(i)-1;
while(it_l=0){
o[it_r]=acc[it_a];
it_r++;
这是一个--;
}
/*it_r+=1;此处不需要将其作为
在上述循环中,它已经递增*/
o[it_r]=32;
it_r+=1;/*上述增量语句已移到此处(仅限
将新值加载到输出数组时递增)*/
它=0;
strcpy(附件“”);
}
it_l++;
}
/*下面的部分反转并存储
未在上述回路中运行的输入*/
it_a-=1;
while(it_a>=0){
o[it_r]=acc[it_a];
it_r++;
这是一个--;
}
o[it_r]='\0';//端接输出阵列
printf(“%s”,o);
}
上述代码将按预期工作,但存在一些小问题(如下所示)
没有临时存储的版本
- 从开始(0)读取输入字符串
- 将输入中的任何单词复制到输出中,从输出的末尾开始-单词不会反转
- 复制输出中的任何空格,从输出中的位置开始,从末尾开始
#定义大小100//输入数组和输出数组的大小
int main(){
字符i[大小];
字符o[大小];
printf(“输入一个句子:”);
fgets(i,SIZE,stdin);//fgets代替get
int j,len=strlen(i)-1;//假设您吃了EOL字符
o[len]='\0';//从标记输出字符串的结尾开始
对于(j=0;jI看不到o
在任何地方都会得到一个空终止符,如果它丢失了,至少会引起问题。不是bug,而是'
保证是一个空格字符,而32
不是。知道输入和输出应该是什么样子会很有趣。你能回答这个问题来添加示例吗放入并输出?用可识别的东西填充o
,如?”
,并观察结果。密切注意它。\r
。添加一些printf
调试以查看发生了什么。并考虑如何处理第一个单词–前面可能没有空格。请阅读的文档>gets()
。也就是说,对于a,您的代码不应该包含任何输入,除非输入本身是问题所在。请用一些硬编码值替换它,这样就没有人需要解释或猜测任何内容来重现您的问题。您的程序反转整个输入字符串。我认为OP希望反转单词,即。“Hello World”将导致“World Hello”“。我想他是想把句子的字母颠倒过来。如果你看一下OPs代码,你会发现OP复制了一个单词,把它颠倒过来,然后再把这个单词颠倒过来,使它恢复到正常的形式。很晚才回复,,,对不起。是的,用户4386427是对的。我想把单词改为重新排序。”
o[it_r] = ' '; // Use ' ' instead of 32
it_r += 1;
#include <stdio.h>
#include <string.h>
#define SIZE 100 //The size of input array is prefered to be equal to ouput array.
int main(){
char input[SIZE];
char output[SIZE];
int i = 0, j = 0;
//i is the iterator to the input sentence,
//j is the iterator to the output sentence
printf("Enter a sentence:");
gets(input);
int len = strlen(input) - 1; //Total length.
j = len;
while(input[i]!= NULL){
output[j] = input[i];
i++;
j--;
}
output[len+1]= NULL;
printf("%s", output);
printf("Finished");
}
#include <stdio.h>
#include <string.h>
#define SIZE 100
#define ACCUM_CHAR_SIZE 25
int main(){
char i[SIZE];
char acc[ACCUM_CHAR_SIZE];
char o[SIZE];
int it_l = 0, it_a = 0, it_r = 0;
printf("Enter a sentence:");
gets(i);
int len = strlen(i) - 1;
while(it_l <= len){
if(i[len - it_l] != ' '){
acc[it_a] = i[len - it_l];
it_a++;
}
else{
it_a -= 1;
while(it_a >= 0){
o[it_r] = acc[it_a];
it_r++;
it_a--;
}
/*it_r += 1; There is no need to increment it_r here as
it is already incremented in the above loop*/
o[it_r] = 32;
it_r += 1; /* The above increment statement was moved here(only
incremented when a new value is loaded in to output array) */
it_a = 0;
strcpy(acc, "");
}
it_l++;
}
/* The below section reverses and stores the first word of the
input which was not getting operated in the above loop */
it_a -= 1;
while(it_a >= 0){
o[it_r] = acc[it_a];
it_r++;
it_a--;
}
o[it_r] = '\0'; // Terminating output array
printf("%s", o);
}
#define SIZE 100 //size of the input array and output array
int main(){
char i[SIZE];
char o[SIZE];
printf("Enter a sentence: ");
fgets(i, SIZE, stdin); // fgets instead of gets
int j,len = strlen(i) - 1; // assuming you eat the EOL character
o[len] = '\0'; // start by marking end of output string
for(j=0 ; j<len ; j++) { // navigating the input string
if (i[j] == ' ') {
o[len-j-1] = i[j]; // if space, copy (from end) in output
}
else {
int k=j;
do {
k++; // count(+1) the word, non-space, characters
} while (k<len && i[k] != ' ');
strncpy(&o[len-k], &i[j], k-j); // copy the word
j = k-1;
}
}
printf("%s\n", o);
}