Input: Hello there boy(any 80 character string)
逻辑错误
Expected output: boy there Hello
Current output: (nothing - does compile though) \
我的HW提示:
收件,提示用于由一个或多个空格分隔单词序列的用户的程序,每个单词都是一个字母数字字符序列。您可以假定输入的文本字符串长度不超过80个字符,每个单词长度不超过20个字符。使用fgets()命令读取输入文本字符串。例如,我们可以声明一个char数组char [81];然后使用fgets(句子,81,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;
}
这是什么意思'我==”“'你正在做in'如果'条件 – Omkant
再次看我认为它应该是句[我] ==''。我试图找出单词之间的所有空格。防爆。嗨'_'there'_'boy –
如果它不能改变问题的目的,或者问题仍然存在,您可以随时编辑您的帖子并修复错误。 – ForceMagic