-1

Input: Hello there boy(any 80 character string)

Expected output: boy there Hello

Current output: (nothing - does compile though) \

我的硬件提示:

编写一个程序,提示用户输入由一个或多个空格分隔的单词序列,其中每个单词都是字母数字字符序列。您可以假设输入的文本字符串不超过 80 个字符,每个单词不超过 20 个字符。使用 fgets() 命令读取输入文本字符串。例如,我们可以声明一个 char 数组 char sentence[81]; 然后使用 fgets(sentence,81,stdin); 从标准输入 stdin(即键盘输入)中读取最多 80 个字符到字符数组 sentence[] 中,这还将在数组末尾插入一个空字符(字符串终止字符);这就是为什么数组需要比输入的文本长一个字节的原因。您的程序应该以相反的顺序打印出单词,与输入的完全相同,每个单词之间只有一个空格。您可以假设没有输入标点符号或控制字符。您的程序应命名为 reverse_words.c,并且您的输出应与以下示例中显示的输出完全匹配。

我已经查看了其他示例,只是尝试使用我所知道的来制作这个程序。对我来说,它似乎工作,但它没有。有人可以帮我找到我的逻辑在哪里吗?

#include <stdio.h>


int main()
{
   char sentence[81];
   char space[81];
   int i , h = 0, j, start;

   printf("Enter a sentance (up to 80 characters): ");
   fgets(sentence,81,stdin);

   //starting backwards go from element 80 to find first non space
   //make array to store element numbers of sentence in new array
   for(i = 80; i >= 0; i--)
   {
     if(sentence[i] != ' ' || sentence[i] != '\0')
     { start = i;
       //printf("%i", start);
     }
    if(i < start && i == ' ')
   {
     space[h] = i;
     h++;
  }
}

h = 0;

  //start at first space and print characters till next space, repeat till all words printed
  for( j = space[h]; j < space[h + 1]; h++)
  {
     printf("%c", sentence[j]);
     if (j == space[h + 1])
        printf(" ");
  }
  return 0;
}
4

5 回答 5

2

粗略一瞥

if(i < start && i == ' ')

改成

 if(i < start && sentence[i] == ' ')

您可以改进的其他方面:

不要从 80 开始循环,而是找到输入的字符串的长度,然后倒退。利用strlen

于 2012-11-12T07:26:01.160 回答
0

尝试这个:

  #include <stdio.h>
  #include <conio.h>

void main()
{
   int x=0,y=0,t,i=0,j=1,k=0,p[10];
   char a[80],b[80];
   clrscr();
   printf(" enter a string ");
   gets(a);
   p[0]=0;
   //count char in string
   while(a[i]!= '\0')
   {
      i++;
      //array p[] note the space position in string
      if(a[i]==' ')
      {
         p[j]=i;
         j++;
      }
   }
   k=i;
   t=0;
   //loop till space to next space
   for(j=j-1;j>=0;j--)
   {    
      x=p[j];
      y=x;
      //reposition the words
      while(x<k)
      {
         // put space when it come to first position
         // because at the beginning there is no space
         if (x==0)
         {
            b[t]= ' ';
            t++;
         }
         b[t]=a[x];
         t++;
         x++;
      }
      k=y;
   }
   puts(b);
   getch();
}
于 2012-11-12T09:19:24.387 回答
0

正如我在评论中所说,这是一个完美的递归问题。这就是为什么我想分享这个解决方案。当然,除非您完全了解正在发生的事情,否则不要使用它:)

#include <stdio.h>

void PrintWordsInReverseOrder(char* sentence)
{
    // Search beginning of word (skipping non-readable characters <= ' ')
    char* start = sentence;
    while ((*start != '\0') && (*start <= ' ')) start++;
    if (*start == '\0') return; // this is the end my friend
    // Search end of word (skipping readable characters > ' ')
    char* end = start;
    while (*end > ' ') end++; // will also stop at '\0'
    if (*end != '\0')
    {   // We are not at the end of the string, so there might be a next word available.
        // Print the next word using recursion (this causes to print out the last word first)
        PrintWordsInReverseOrder(end + 1);
    }
    char endBackup = *end;
    *end = '\0'; // temporary terminate the word so we can print it out (don't be affraid, we have a backup in endBackup)
    printf(start);
    printf(" ");
    *end = endBackup; // restore word termination char
}

int main()
{
    char sentence[81];
    printf("Enter a sentance (up to 80 characters): ");
    fgets(sentence, sizeof(sentence), stdin);
    PrintWordsInReverseOrder(sentence);
    return 0;
}
于 2012-11-12T08:04:33.613 回答
0

这部分代码可以稍微清理一下:

//starting backwards go from element 80 to find first non space
//make array to store element numbers of sentence in new array

而不是从 80 个字符返回,只需使用strlen()of sentence

printf("Enter a sentence (up to 80 characters): ");
fgets(sentence, 81, stdin);
start = (int) strlen(sentence) - 1; /* subtract one to account for null character */

然后从该值返回:

for (i = start; i >= 0; i--) {
    /* ... */
}

您还可以通过定义和使用常量来表示句子的最大长度来提高代码的总体质量:

#define MAX_SENTENCE_LENGTH 80
...
char sentence[MAX_SENTENCE_LENGTH + 1];
char space[MAX_SENTENCE_LENGTH + 1];
...
printf("Enter a sentence (up to %d characters): ", MAX_SENTENCE_LENGTH);
fgets(sentence, MAX_SENTENCE_LENGTH + 1, stdin);

然后,如果您想使用不同的限制,您只需在一处更改代码。

于 2012-11-12T07:30:25.150 回答
0

这是尝试运行的代码:

int len=strlen(sentence);
for(i=0;i<len;i++)
   space[i]=sentence[len-1-i];
space[i]='\0'
int start=0,end;
for(i=0;i<=len;i++)
{     
   if(space[i]!=' ' || space[i]!='\0')  //edited in place of && it should be ||
   {}
   else 
   {
      end=i-1;
      str_rev(&space[start],&space[end]);
      start=i+1;
   }
}


void str_rev(char *s,char*e)
{
   while(s>e)
   {
      char tmp=*s;
      *s=*e;
      *e=tmp;
      s++;
      e--;
   }
}
于 2012-11-12T07:34:48.963 回答