Use scanf instead of fgets and fputs in my program in C?

There is no fputs and only a single call to fgets in this code.

Up vote -1 down vote favorite share g+ share fb share tw.

{ return isPalindrome( str+1, length-2 ); /* continue checking */ } else { return 0; /* comparison failed, not a palindrome */ } } void strToUpper( char *src ) { /* convet to upper case any letter */ while( ( *src = toupper( *src ) )! = '\0' ) { ++src; } } int main( void ) { int result = 0; char str40 = { '\0' }; printf( "Please type the string to identify Palindrom or not: ", stdout ); fflush( stdout ); fgets( str, sizeof str, stdin ); strToUpper( str ); /* make all letters the same for comparison */ result = isPalindrome( str, ( strlen( str ) - 1 ) ); /* recursive starts here */ if( result == 1 ) { printf( "1" ); } else { printf( "0" ); } getchar(); return 0; } I wrote this code which identifies palidromes in C, but I want to use scanf instead of fputs or fgets, so I can understand the other way to solve this problem. C link|improve this question edited Oct 24 '11 at 20:14middus5,722818 asked Oct 24 '11 at 3:36Rex Ryan15.

1 The scanf functions should never be used for anything. Also, when you put spaces on the inside of your parentheses you make the baby Jesus cry. – Zack Oct 24 '11 at 3:41 Why are you so insistent on fixing your code when it clearly already works?

This is the second question you've asked with this piece of code. Why not test it and see if it has any problems before you go trying to correct it? – Chris Lutz Oct 24 '11 at 3:42 @Zack - I'm glad to see someone agree with me about the parenthesis thing.

I wince every time I see it. – Chris Lutz Oct 24 '11 at 3:43.

There is no fputs and only a single call to fgets in this code. The method of input here really don't affect "way to solve this problem". It's the same code - changing one line won't change lot.

Scanf not a safe function. You can do scanf("%s", str) but fgets is better and recommended. If you need to analyze string after fgets, you can use sscanf.

Scanf is safe if used right, but doing so is trickier than introductory C tutorials often teach, and I hope you didn't plan on doing much in the way of error recovery if you get bad input. – Chris Lutz Oct 24 '11 at 3:41 1 And scanf("%s", str) reads a whitespace-delimited word, not a line. If you want to learn how to use scanf, find a problem for which it's suited.

– Keith Thompson Oct 24 '11 at 3:41 Thanx for all the advices but I am trying to make my code for the people to who are begineer in C, so they can learn easily.. Is there anything else should I change to it to make it more simple? – Rex Ryan Oct 24 '11 at 3:46 1 Well, you could stop using printf -- all three calls are incorrect, albeit in two different ways. The use of getchar at the end of the program is also a bad practice.

The initialization of str is unnecessary. And if you're going to go with open curly braces on their own lines, then you should omit the braces entirely around single-statement blocks. – Zack Oct 24 '11 at 16:20 1 Also, recursion is a notoriously difficult concept for people new to programming.

If your audience is learning C having already mastered a higher-level language, that's not a problem, but if you're teaching C to people new to programming, (a) that's a bad idea in itself, (b) you might want to consider changing isPalindrome to use iteration instead. – Zack Oct 24 '11 at 16:24.

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.

Related Questions