为什么这种可逆语句算法不起作用?

时间:2018-09-22 04:32:34

标签: 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);
}

程序在理论上看起来不错,但是在执行时,有时会打印垃圾值,仅显示某些单词或句子,但仅用垃圾值而不是空格将其反转一半。

上面的程序是将每个单词保存到温度,然后将反向温度(存储单词时温度反转)返回到输出。但是,它失败了。

谢谢您的帮助。

4 个答案:

答案 0 :(得分:1)

至少有三个问题。

第一个问题是,您永远不会终止字符串o来进行更改:

   printf("%s", o);

进入

   o[it_r] = '\0';
   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

答案 1 :(得分: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");
 }

答案 2 :(得分:0)

请尝试使用下面给出的修改后的代码,对已更改的部分进行了注释(为便于阅读,删除了所有其他注释)

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

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

  1. 使用gets():-不建议将gets用于输入字符串。有关更多信息,请参见此link

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

答案 3 :(得分:0)

没有临时存储的版本。

  • 从开头(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] = '\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);
}