2011-10-13 29 views
0
#include<stdio.h> 
#include<stdlib.h> 

#define n ((sizeof(char)) * 100) 

int stringlength(char * str) 
{ 
    int count=0; 
    while(*str) 
    { 
     if(*str == '\n') 
     { 
      *str=0; 
     } 
     else 
      count++, str++; 
    } 
    return count; 
} 


int palin1(char *str, int k) 
{ 
    char * pend = str + k - 1; 
    if(*pend != *str) 
     return 0; 
    else 
     palin1(str+1, k-1); 
     return 1; 
}  

int palin(char *str) 
{ 
    int length = stringlength(str), f=0; 
    char *pend = str + length - 1; 
    while(str <= pend) 
    { 
     if(*str == *pend) f=1; 
     else 
      return (f = 0); 
     str++, pend--; 
    } 
    return 1; 
} 

main() 
{ 
    char * ps = (char *)malloc(n); 
    int flag; 
    if(ps == NULL) printf("Malloc Fail\n"); 
    else 
    { 
     printf("Malloc Succeeded, you have memory of %d bytes\n", n); 
     printf("This program checks if String is Palindrome or not\n\ 
     \nEnter your String: "); 
     fgets(ps, 100, stdin); 
     printf("You entered: %s of length %d", ps, stringlength(ps)); 
     int i = 0; 
     printf("\n\nEnter:\n1.Using iteration\n2.Using Recursion "); 
     scanf("%d", &i); 
     switch(i) 
     { 
      case 1: 
       flag=palin(ps); 
       break; 
      case 2: 
       flag=palin1(ps,stringlength(ps)); 
       break; 
      default: 
       printf("Invalid input"); 
     } 

     if(flag) printf("\nYou entered a Palindrome"); 
     else printf("\nNot a Palindrome"); 
    } 
    free (ps); 
    return 0; 
} 

为什么上面的程序http://www.ideone.com/qpGxi不给上把输入的任何输出:程序异常终止,如果输入是非常大的

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

我知道fgets(ps,100,stdin)将只需要100字符而不是更多,但为什么程序停止执行?

+3

这只是真棒: - ))#define n((sizeof(char))* 100)' – cnicutar

+0

哈哈,是的,这是非常不必要的。我从来没有见过(sizeof(char))不等于1的系统。 – Chriszuma

+3

@Lohit:我建议你不要定义小写字母标识符。我曾经用一个'#define'来调试学员代码,当他引入一个名称相同的变量时,开始无法编译......花了我很多时间来找到它! – pmg

回答

1

根据fgets spec的推荐,您应该检查fgets故障。

if (fgets(ps,100,stdin) == NULL) { 
    printf("Input failed."); 
    //check for 'feof' or 'ferror' here 
    return -1; 
} 
printf("You entered: %s of length %d",ps,stringlength(ps)); 

我不明白为什么fgets会失败,但你会得到一个未初始化字符缓冲区回来,将导致程序崩溃printf

编辑:你也应该注意你的编译器警告。

prog.c:49: warning: return type defaults to ‘int’ 
prog.c: In function ‘main’: 
prog.c:59: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result 
prog.c:63: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result 
prog.c: In function ‘palin’: 
prog.c:46: warning: control reaches end of non-void function 
prog.c: In function ‘main’: 
prog.c:52: warning: ‘flag’ may be used uninitialized in this function 

你可以看到,即使你的编译器建议检查fgets为空。此外,在默认情况下,flag应设置为0,否则如果用户输入的内容不是12,则会出现未定义的行为。

编辑2:为了基督的缘故哦!你的程序工作正常!你忘了在Ideone中检查“run program” !!!

http://www.ideone.com/7ecZd

0

它的终止,因为留在输入流中,如果输入过大的字符。例如,如果你希望只需要使用fgets字符,但已经给出了输入作为 -

StackOverflow的

Overflow留在输入流。他们需要从流中删除,以便进一步的输入操作成功。因此,从使用流去掉那些多余的字符 -

fgets(ps,100,stdin); 
while (getchar() != '\n'); 

由于输入流与违规字符来袭,scanf语句其实需要用户输入不工作,并跳转到后续操作。

还初始化flag变量为其他方式它有垃圾值。

+0

为什么不只是'fflush(stdin)'如果输入流真的包含了某些内容? –

+0

因为'fflush'只为输出或更新流定义;它不会清除输入流。 –

+0

@JohnBode你可以提供一些我可以读到的有关fflush的内容,它明确指出fflush没有为输入流定义?我一直在寻找,但没有得到任何东西。 –

0

你不能打破一个字符串就这样

printf("%s\n", "string literal **WRONGLY**\n 
broken right after the line break."); 

你可以做的是利用加入连续字符串文字的预处理功能,使只有一个

printf("%s\n", "string literal **CORRECTLY**\n" 
"broken because the preprocessor joins these 2 parts.");