Write a C function to check whether a string is palindrome or not using a stack.

Write a main program to test your function.

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction. A few examples of palindrome strings are: madam, dad and radar.


Difficulty level
Video recording
This exercise is mostly suitable for students
int ispalindrome(char *str)
{
 	int i, count = 0, len;
	stack s = CreateStack();
	len = strlen(str);
    	for (i = 0; i < len ; i++)
    	{
       		Push(&s, str[i]);
   	}

    	for (i = 0; i < len; i++)
    	{
		Top(s,&e);
		Pop(&s);
        	if (str[i] == e)
            		count++;
    	}

    	if (count == len)
		return 1;
    	else
       	 	return 0;
}


int ispalindrome2(char *str)
{
    stack s;
    int length , i;
    element e;
    
    s=CreateStack();
    
    length = strlen(str);
    
    for(i=0;i<length/2;i++)
        Push(&s,str[i]);
    
    if(length%2==0)
        length--;
        
    for(i=length/2+1 ; str[i]; i++)
    {
        Top(s,&e);
        Pop(&s);
        if(str[i]!=e)
            return 0;
    }
     return 1;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Insert a value into a sorted array