English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

//USING DATA STRUCTURES
#include
#include
#include
#include
typedef struct stack
{
char c;
struct stack *link;
}S;
static int length;

void push(S** top,char ch)
{
S* temp=(S*)malloc(sizeof(S));
temp->c=ch;temp->link=NULL;
if(*top==NULL)
(*top)=temp;

else
{
temp->link=(*top);
(*top)=temp;
}
length++;
}

char pop(S** top)
{
if((*top)==NULL)
{
printf("STack empty");
return NULL;
}
S* temp=(*top);
char ch=temp->c;
(*top)=(*top)->link;
free(temp);
return ch;
}

void inserttostack(S** top,char str[])
{
for(int i=0;i push(top,str[i]);
}
void isitpalindrome(S** top,char str[])
{
for(int i=0;i if(str[i]!=pop(top))
{
printf("\n%s:Not Palindrome",str);
return;
}
if(length%2==0)
printf("\n%s: Even Palindrome",str);
else
printf("\n%s: Odd palindrome",str);
}
void display(S* top)
{
while(top!=NULL)
{
printf("%c",top->c);
top=top->link;
}
}

void main()
{
S *top=NULL; char str[20];
printf("Enter a string:");
gets(str);
inserttostack(&top,str);
printf("\n\nstack length: %d\nSTACK: ",length);display(top);
isitpalindrome(&top,str);
}

2007-02-17 20:46:51 · answer #1 · answered by KillingJoke 3 · 0 0

I'm bored so I'll write it up for you. Make sure to do your own homework though :P to reverse a string you must get the string size, so... char myString[] = "DudeNoWay\0"; int size = 0; while(myString[size]) ++size; the variable size should now equal the size of the string. (Notice that all I needed to do was while(myString[size]) because the while loop will become false when it reaches the null character. while(myString[size]) is the same as while(myString[size] != '\0')) Now that we have the size of the string, we can do: int a; for(a = size;a != -1;--a) { printf("%c", myString[a]); } and there you go. Now to test if it is a palindrome... a = size - 1; size -= 1; int b = 0; int check = 0; while(b != size + 1) { if(myString[a] == myString[b]) check = 0; else check = -1; if(check == -1) { printf("%s", "Not a palindrome!\n"); return -1; } --a; ++b; } printf("%s%s", myString, " is a palindrome!\n"); But notice that this program is buggy. Somehow the letter in the middle of a word is not being checked, so racecar and racbcar come up as a palindrome. You have to fix the values of a and b. Plus the size - 1 to fix the offset seems to be a bit sloppy, but I thought I would post the code anyways to give you a general idea of what to do. :)

2016-03-29 00:59:44 · answer #2 · answered by ? 4 · 0 0

I guess you have to use a stack instead of writing a recursive function?

Here's some pseudo-code ...

checkPalindrome(String s){
Stack stack = new Stack();
stack.push(s);
while (stack is not empty){
String tmp = stack.pop(); // get the last element from the stack
// and remove it.
if (tmp.length == 0)
retValue = EVEN;
else if (tmp.length == 1)
retValue = ODD;
else if (tmp.firstChar() != tmp.lastChar)
retValue = NOT_A_PALINDROME;
else {
tmp = substring(tmp, 1, tmp.length-2); // tmp without the first
// and last characters;
stack.push(tmp);
}// else
}// while
}// CheckPalindrome

2007-02-17 19:52:39 · answer #3 · answered by Amit Y 5 · 0 0

fedest.com, questions and answers