[Courses] [C] What is wrong here??

Suzi Anvin suzi at anvin.org
Thu Aug 15 11:51:06 EST 2002

> b). I then decided to use string functions to conclude that a string is a
> palindrome or not. I wanted to avoid using readymade functions for reversing
> a string. So, thought of writing one. And that is where, things got badly
> messed up. 

Out of curiosity, why?  The first potion, that includes just one array, 
sounds inherently simpler to me, and I'm a BIG proponent of the KISS 
school of programming (and writing, and...)  :)  as soon as you make 
things more complicated than they need to be, you're bound to be 
confused.  Plus, copying the whole string to another variable, in 
whatever order, takes so much time!  If you're using your first option, 
it can be very quick...  if first and last letters are different, you 
pop out of the loop with a "no palindrome" message right there.  At 
worst, you'll only deal with strlen/2 comparisons, rather than having to 
copy ALL the characters, then compare them.

BTW on string reversals and such, I was just assigned this by my hubby 
for my own learning pointers education, you can see what I hacked up at 
http://www.anvin.org/~suzi/C_files/text_reordering.c  I can't promise it 
is the best way to do things as I'm very new to this myself, but it does 

> char strrev(char array[]);
> int main()
> {
>         char string1[10];
>         //char *string1=string;

random quetion...  what are the // doing before the char?

FOr my own lack of confusion, I like to 1) never have apointer and a non 
pointer variable have the same name.  To my reading here you have an 
array of chars and a char pointer named string1.  huh?

Also, to save myself confusion and to make sure I know exactly where a 
pointer is pointing, I initialize my pointers separate from their 
declaration, like this: string1 = &string[0]  It so helps me when I go 
to draw them to have started with a written record of where they are!

>         //char *temp;
>         fflush(stderr);
>         fprintf(stderr,"\nEnter a string: ");
>         fflush(stderr);
>         fflush(stdin);
>         fgets(string1,sizeof(string1),stdin);
>         printf("\nThe reversed string is: %s\n",strrev(string1));
>         if(strcmp(temp,strrev(string1))==0)

Problem here - what is temp pointing at?  have you defined this yet?  If 
so I missed it. I'm also wondering why you're reversing the string 
again?  it looks to me like you've already done so, in the printf call 
above.  If you reverse it AGAIN it goes back in the original order and 
of course the two will be the same!

>         {
>         printf("\nPalindrome\n");
>         }
>         else
>         {
>         printf("\nSorry\n");
>         }*/
> char strrev(char array[])
> {
>         register int i;
>         for(i=strlen(array)-1;i>=0;i--)
>         {
>                 return *array;

Others have already commented that you can't return a pointer unless the 
function is declared as a pointer.  Even given that, I'm missing what 
the function DOES.  How do you change/move the pointer here?  I am not 
seeing anything that changes where *array is pointing.  Only the counter 
is changing...

>         }
> }
> Thanks,
> Andy
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses

More information about the Courses mailing list