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

Anand R anand.r at cybertech.co.in
Tue Aug 13 18:49:09 EST 2002


Hi team,

		I struggled for over an hour with this program trying to
clear my concepts on pointers, arrays, functions etc. I stumbled upon an
idea to write a program which will figure out whether a given string is a
palindrome or not. That was the simplest I could start with, guys. 

a). I tried writing the palindrome program, the easy way and it has already
worked. Using a single for loop and then use the if condition.

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. 

My purpose is to accept a string input from the user, and check whether it
is a palindrome or not. I tried hard to figure out how to write a C function
to reverse a string, and how to store this reversed value in another string.
If somebody could tell me where I am going wrong here. Any pointers to
understand this better would be greatly appreciated. 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char strrev(char array[]);
int main()

{
        char string1[10];
        //char *string1=string;
        //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)
        {
        printf("\nPalindrome\n");
        }
        else
        {
        printf("\nSorry\n");
        }*/


char strrev(char array[])
{
        register int i;
        for(i=strlen(array)-1;i>=0;i--)
        {
                return *array;

        }
}

Thanks,
Andy



More information about the Courses mailing list