Enrique
Enrique

Reputation: 11

Palindrome in C without pointers and recursion

I'm trying to determine if a phrase is a palindrome (a word that is the same from left to rigth) or not but i can't make it work. What's wrong?, i can't use pointers or recursion or string type variables

#include <stdio.h>

#include <string.h>

int main()

{

 int i,j = 0,length;
 char space = ' ';
 char phrase [80],phrase2[80],phrase3[80];

 printf("Give me the phrase: ");
 gets(phrase);
 length = strlen(phrase);

 for(i =0; i <= length - 1; i++)
 {
  if(phrase[i] != space)    //Makes the phrase without spaces
  {
   phrase2[i] = phrase[i];
   j++;
  }
 }

 for(i = length -1; i >= 0;i--)
 {
  if(phrase[i] != space)    //Makes the phrase backwards an without spaces
  {
   phrase3[j] = phrase[i];
   j++;
  }
 }

 length = strlen(phrase2);

 for(i =0; i <= length -1;i++)      //Compare the phrases to know if they are the same
 {
  if(phrase2[i] != phrase3[i])
  {
   printf("It's not a palindrome\n"); 
   return 0;
  }
 }
 printf("It's a palindrome\n");
 return 0; 
}

Upvotes: 1

Views: 1361

Answers (5)

Nik Bougalis
Nik Bougalis

Reputation: 10613

Why not use a std::stack? You will need two loops, each iterating the length of the input string. In the first loop, go through the input string once, pushing each character ont the stack. In the second loop, pop a character off the stack and compare it with the character at the index. If you get a mismatch before the loop ends, you don't have a palindrome. The nice thing with this is that you don't have to worry about the even/odd length corner-case. It will just work.

(If you are so inclined, you can use one stack (LIFO) and one queue (FIFO) but that doesn't substantially change the algorithm).

Here's the implementation:

bool palindrome(const char *s)
{
    std::stack<char> p; // be sure to #include <stack>

    for(int i = 0; s[i] != 0; i++)
        p.push(s[i]);

    for(int i = 0; s[i] != 0; i++)
    {
        if(p.top() != s[i])
            return false; // not a palindrome!

        p.pop();
    }    

    return true;
}

Skipping spaces is left as an exercise to the reader ;)

Upvotes: -1

rajatbarman
rajatbarman

Reputation: 149

This is what I came up with:

#include <stdio.h>
void main() {
char a[50],b[50];
int i=0,j,ele,test=0,x;
while((a[i]=getchar())!='\n') {
if(a[i]!=' ' && a[i]!=',') //do not read whitespaces and commas(for palindromes like "Ah, Satan sees Natasha")
i++;
}
a[i]='\0';
ele=strlen(a);
// Convert string to lower case (like reverse of Ava is avA and they're not equal)
for(i=0; i<ele; i++)
if(a[i]>='A'&&a[i]<='Z')
a[i] = a[i]+('a'-'A');
x = ele-1;
for(j=0; j<ele; j++) {
b[j] = a[x];
x--;
}
for(i=0; i<ele; i++)
if(a[i]==b[i])
test++;
if(test==ele)
printf("You entered a palindrome!");
else
printf("That's not a palindrome!");
}

Probably not the best way for palindromes, but I'm proud I made this on my own took me 1 hour :( lol

Upvotes: 0

Andrew Cooper
Andrew Cooper

Reputation: 32586

Try this:

 for(i =0, j=0; i <= length - 1; i++)
 {
  if(phrase[i] != space)    //Makes the phrase without spaces
  {
   phrase2[j] = phrase[i];
   j++;
  } 
 }

 for(i = length -1, j = 0; i >= 0;i--)
 {
  if(phrase[i] != space)    //Makes the phrase backwards an without spaces
  {
   phrase3[j] = phrase[i];
   j++;
  }
 }

 length = j;

Update

In response to Praetorian's post here's the code to do it without copying the string.

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

int main()
{
  int i, j, length;
  char space = ' ';
  char phrase[80];

  printf("Give me the phrase: ");
  gets(phrase);
  length      = strlen(phrase);

  for( i = 0, j = length - 1; i < j; i++, j-- ) {
    while (phrase[i] == space) i++;
    while (phrase[j] == space) j--;
    if( phrase[i] != phrase[j] ) {
      printf("It's not a palindrome\n");
      return 0;
    }
  }

  printf("It's a palindrome\n");
  return 0; 
}

Upvotes: 2

Praetorian
Praetorian

Reputation: 109189

Your question has already been answered by others but I'm posting this code to show that it is not necessary to make the phrase3 copy to hold the reversed string.

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

int main()
{

  int i, j, length, halfLength;
  char space = ' ';
  char phrase1[80], phrase2[80];

  printf("Give me the phrase: ");
  gets(phrase1);
  length      = strlen(phrase1);

  for( i = 0, j = 0; i <= length; ++i ) {
    if( phrase1[i] != space ) {    //Makes the phrase1 without spaces
      phrase2[j++] = phrase1[i];
    }
  }

  length      = strlen(phrase2);
  halfLength  = length / 2;

  for( i = 0, j = length - 1; i < halfLength; ++i, --j ) {
    if( phrase2[i] != phrase2[j] ) {
      printf("It's not a palindrome\n");
      return 0;
    }
  }

  printf("It's a palindrome\n");
  return 0; 
}

Upvotes: 1

Alex Florescu
Alex Florescu

Reputation: 5151

Before the 2nd loop you want to set j=0. It should work after that.

PS: If you debugged by printing out your three strings, you would've figured it out in a matter of minutes. When you don't know what goes wrong, print out the values of variables at intermediate steps, so you know where your problem occurs and what it is.

Upvotes: 1

Related Questions