2012-11-12 208 views
0

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; 
} 
+0

这是什么意思'我==”“'你正在做in'如果'条件 – Omkant

+0

再次看我认为它应该是句[我] ==''。我试图找出单词之间的所有空格。防爆。嗨'_'there'_'boy –

+0

如果它不能改变问题的目的,或者问题仍然存在,您可以随时编辑您的帖子并修复错误。 – ForceMagic

回答

2

从粗略地看一眼

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

更改为

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

其他的事情你可以改善:

80不循环,而不是发现输入的字符串的长度然后倒退。使用strlen

0

的代码,这部分可以被清理了一下:踏着由80个字符后面的

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

相反,只取sentencestrlen()的:

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

然后,如果你想用不同的限制,你只需要改变代码在一个地方。

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--; 
     } 
    } 
+0

有一些警告... reverse_words.c:67:39:警告:多字符字符常量 reverse_words.c:函数'main'中: reverse_words.c:71:警告:隐式声明函数'str_rev' /tmp/ccdZzmLZ.o:函数'main': reverse_words.c :(.text + 0x138):未定义的引用'str_rev' collect2:ld返回1退出状态 –

+0

尝试声明'str-rev ()'在调用'str_rev()'之前在顶部,并且编辑代替'&&'它应该是'||' – Omkant

+0

如果实际定义低于调用环境,则始终提供函数声明 – Omkant

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; 
} 
+0

一级棒!谢谢,但我有点失去了指针。我的硬件是星期二到期的,所以我仍然有时间查看这个,但现在我必须去。如果你可以自由地帮助,我会在更多的指点之后明天发布问题。 –

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 posi 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; 
    //reposi the words 
    while(x<k) 
    { 
     //put space when it come to first posi bcuz at d beginin der is no space 
     if (x==0) 
     { 
     b[t]= ' '; 
     t++; 
     } 
     b[t]=a[x]; 
     t++;x++; 
    } 
    k=y; 
} 
puts(b); 
getch(); 
}