You should check for fgets failure, as recommended by the fgets spec .
Up vote 0 down vote favorite share g+ share fb share tw.
Include #include #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 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; } Why does the above program ideone.com/qpGxi does not give any output on putting the input: mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm I know fgets(ps,100,stdin) will take only 100 characters and not more than that, but why does the program halt execution? C link|improve this question edited Oct 14 '11 at 5:40Brendan Long10.6k1745 asked Oct 13 '11 at 14:13Lohit368111 92% accept rate.
3 This is just awesome :-)) #define n ((sizeof(char)) * 100 ) – cnicutar Oct 13 '11 at 14:21 Haha, yeah that is pretty unnecessary. I've never seen a system where (sizeof(char)) wasn't equal to 1. – Chriszuma Oct 13 '11 at 14:24 3 @Lohit: I suggest you do not define lower case identifiers.
I've once had to debug a trainee code with such a #define which started to fail compilation when he introduced a variable with the same name ... took me a lot of time to find it! – pmg Oct 13 '11 at 14:26.
You should check for fgets failure, as recommended by the fgets spec. 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)); I don't see why fgets would be failing, but you would get an uninitialized character buffer back, which would crash printf.
EDIT: You should really pay attention to your compiler warnings, too. 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 You can see that even your compiler recommends checking fgets for null. Also, flag should be set to 0 in the default case, otherwise you will get undefined behavior if the user enters something other than 1 or 2. EDIT 2: Oh for Christ's sake!
Your program works fine! You forgot to check "run program" in Ideone! ideone.com/7ecZd.
You cannot break a string literal just like that printf("%s\n", "string literal **WRONGLY**\n broken right after the line break. "); What you can do is use the preprocessor feature of joining successive string literals to make just one printf("%s\n", "string literal **CORRECTLY**\n" "broken because the preprocessor joins these 2 parts. ").
It's terminating because there are characters left in the input stream if the input is too large. For example, if you wish to take only 5 characters using fgets but have given the input as - StackOverflow Overflow are left in the input stream. They need to be removed from the stream for further input operations to succeed.
So, remove those extra characters from the stream using - fgets(ps,100,stdin); while (getchar()! = '\n'); Since the input stream is struck with offending characters, the scanf statement that actually takes the user input is not working and jumping to subsequent operations. Also initialize the flag variable to 0 other wise it has garbage values.
– Lohit Oct 13 '11 at 15:11 Because fflush is only defined for output or update streams; it doesn't clear out the input stream. – John Bode Oct 13 '11 at 16:18 @JohnBode can you provide something which I can read about fflush, which explicitly states fflush is not defined for input streams? I have been looking but not getting anything.
– Lohit Oct 13 '11 at 17:28 @Lohit: From the language standard, 7. 19.5.2/2: "If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined. " – John Bode Oct 13 '11 at 19:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.