views:

145

answers:

3

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; 
}
+2  A: 

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; 
}
Andrew Cooper
wow finally, thanks bro
Enrique
@Enrique - please accept the answer
Andrew Cooper
+1  A: 

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.

anothem
+1  A: 

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; 
}
Praetorian
It's also possible to do it in a single loop without copying the string at all. You just have to check for spaces and adjust the indexes appropriately as you parse the string from each end.
Andrew Cooper