Input: Hello there boy(any 80 character string)
Expected output: boy there Hello
Current output: (nothing - does compile though) \
我的硬件提示:
编写一个程序,提示用户输入由一个或多个空格分隔的单词序列,其中每个单词是一个字母数字字符序列。您可以假设输入的文本字符串长度不超过80个字符,每个字长度不超过20个字符。使用fgets()命令读取输入文本字符串。例如,我们可以声明一个char数组char语句[81];然后使用fgets(句子,81,stdin);从标准输入stdin(即键盘输入)读入最多80个字符到字符数组句子[],它还将在数组的末尾插入一个空字符(字符串终止字符);这就是数组需要比输入文本长一个字节的原因。您的程序应该以相反的顺序打印出单词,与每个单词之间只有一个空格一样输入。您可以假设没有输入标点符号或控制字符。您的程序应命名为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;
}
答案 0 :(得分:2)
从粗略的一瞥
if(i < start && i == ' ')
更改为
if(i < start && sentence[i] == ' ')
您可以改进的其他事项:
不要从80循环,而是找到输入的字符串长度然后向后。使用strlen
答案 1 :(得分: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()
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);
然后,如果您想使用不同的限制,您只需在一个地方更改代码。
答案 2 :(得分: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--;
}
}
答案 3 :(得分: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;
}
答案 4 :(得分: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();
}