为什么这种反转句子算法不起作用?

  • 本文关键字:句子 算法 不起作用 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];
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);
}

程序理论上看起来不错,但是当它被执行时,它有时会打印垃圾值,只有一些单词,或者句子,这些句子只用垃圾值而不是空格反转了一半。

上面的程序是将每个单词保存到 temp,并将反向 temp(存储单词时 temp 反转(保存回输出。但是,它失败了。

谢谢你的帮助。

#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");
}

至少有三个问题。

第一个问题是你永远不会终止字符串o要执行该更改:

printf("%s", o);

o[it_r] = '';
printf("%s", o);

第二个问题是您不正确地递增it_r。改变

it_r += 1;
o[it_r] = 32;   //put a space

o[it_r] = ' ';  // Use ' ' instead of 32
it_r += 1;

第三个问题是你不处理输入的第一个单词(因为前面没有空格(。我会把这个问题留给你作为练习。

顺便说一句:不要使用gets来读取输入。请改用fgets

尝试下面给出的修改后的代码,更改的部分已被注释(为了可读性,所有其他注释都被删除了(

#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] = ''; // Terminating output array
printf("%s", o);
}

上面的代码将按预期工作,但有一些小问题(如下所述(

  1. 不建议使用 gets((:- get 输入字符串。有关详细信息,请参阅此链接。

  2. 临时数组的大小acc:- 如果输入大于 25 个字符的单词,程序可能会输出垃圾值。

没有临时存储的版本。

  • 从"开始"屏幕读取输入字符串 (0(
  • 从输出的末尾开始,将输入中的任何单词复制到输出中 - 单词不会反转
  • 从输出中的位置复制输出中的任何空格,从末尾开始

法典

#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] = '';             // 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("%sn", o);
}

最新更新