Input a string from a file and tell whether that string is a palindrome.

Printable View

• 12-02-2011
galvadon
Input a string from a file and tell whether that string is a palindrome.
^my homework.

I don't expect you to write it. Expectations are irrelevant.
• 12-02-2011
Adak
"Expectations are irrelevant."

Not a palindrome! :p

This is a great exercise to see the beauty of a pointer, in my opinion (although indices work just as well). One on the left side, one on the right side. What could they be comparing that would be relevant to a palindrome? How would they ever know when to quit?
• 12-03-2011
rajarshi
Quote:

Originally Posted by galvadon
^my homework.

I don't expect you to write it. Expectations are irrelevant.

use the pointer power to reverse the string...and then compare both the string to know whether they are palindrome or not
• 12-03-2011
CommonTater
Quote:

Originally Posted by rajarshi
use the pointer power to reverse the string...and then compare both the string to know whether they are palindrome or not

Wow... hard way!

Easy way... set a pointer to the start of the string, another to the end of the string... test one against the other... if they match increment the first pointer, decrement the last pointer...compare again... exit on the first mismatch or when the pointers cross each other.

Code:

```int IsPalendrome(char *str)   {     char *hp, *tp;     hp = str;     tp = str + strlen(str);       while( tp > hp)       {           if (*tp != *hp)             return 0;           tp--;           hp++;       }     return 1; } // in parent function if (IsPalendrome(string))   printf("This here's yer lucky day pardner... it's a palendrome!");```
• 12-08-2011
galvadon
Are you passing the whole string array in the function call?
• 12-08-2011
galvadon
Derp, I see it.
• 12-08-2011
galvadon
PROBLEM: Not saying "YES" when input is in fact a palindrome. What's wrong?

OUTPUT:

Palindrome? Input string
NO STAR RATS*
NO STAT*
NO level*
NO MALAYALAM*
NO solos*
NO KAYA*
NO CAMUS SEES SUMAC*
NO RADAR*
NO REGAL LAGER*
NO ABCDEFGHIJKLMNOQRSTUTSRQONMLKIHGFEDCBA*
NO STRAW WARTS*
NO deified*
NO ABBA*
NO mAMA ANNA MAMA*
NO kayak*
NO NOEL LEON*
NO RACECAR*
NO GALLEONNOELAG*

Code:

```#include<stdio.h> #include<string.h> int max=50; int getStringF(char[],int,FILE *); int isPalindrome(char[]); main() {  FILE *oF,*iF;  oF=fopen("output.txt","w");  iF=fopen("data6.txt","r");  fprintf(oF,"Palindrome?  Input string\n");  char string[max];  int e,k;  e=getStringF(string,max,iF);  while(e!=-1)  {   int p=strlen(string);   char string2[p];   for(k=0;string[k]!='\0'&&p!=0;k++)   {   string2[p]=string[k];   p--;   }   if((strcmp(string,string2))==0)   fprintf(oF,"        YES  %s*\n",string);   else   fprintf(oF,"        NO  %s*\n",string);   e=getStringF(string,max,iF);   p=strlen(string);  }  fclose(iF);  fclose(oF);    } int getStringF(char string[],int max,FILE *iF) {  char c;  int p=0;  c=getc(iF);  while((c!= EOF)&&(c!='\n')&&(p<max))  {   string[p]=c;   p++;   c=getc(iF);  }  string[p]='\0';  if(c==EOF)   p=-1;  return p;  }```
• 12-09-2011
galvadon
Sorry,

INPUT:

STAR RATS
STAT
level
MALAYALAM
solos
KAYA
CAMUS SEES SUMAC
RADAR
REGAL LAGER
ABCDEFGHIJKLMNOQRSTUTSRQONMLKIHGFEDCBA
STRAW WARTS
deified
ABBA
mAMA ANNA MAMA
kayak
NOEL LEON
RACECAR
GALLEONNOELAG
• 12-09-2011
galvadon
Figured it out.
• 12-09-2011
Amberlampz
I was about to test this myself and see if I could find the error, but it's good to know that you figured it out. I'm not exactly what you'd call a coding guru, but I think it would definitely help if you cleaned up the code a little bit. Just nitpicking here:

- Global variable 'int max = 50' could just be a global constant, so you don't need to keep track of passing another int everywhere.

- Where you assign the arrays could be a separate function.

- This allows your input function to be void, and rather than needing to check for '-1' you can just have a 'while(strlen(string))'. Although it's not really fundamentally different, the logic is a little more clear to somebody reading the code who hasn't seen it before.

- General whitespace "management"

At least in my experience, teachers are very appreciative of code that's well-formatted and very modular. Who knows, you might get extra points for making it look nice :)
• 12-09-2011
Tclausex
A couple of things to fix and improve your code ( I don't know what you've changed since "Figured it out.") I'll just show you a better way to write getStringF()
Code:

```int getStringF( char string[], int n, FILE *iF ) {     char c ;     int p ;     p = 0 ;     while( p < n && (c = getc(iF)) != EOF && c != '\n' )         string[p++] = c ;     string[p] = '\0' ;     if( c == EOF )           if( !p )            // eof AND no string read             return EOF ;           else             ungetc( c, iF ) ;  //string read! - put EOF back and decrease file position indicator for next call     return ( p ) ;  // return length of string or zero if only a '\n' read }```
This will now correctly read the last word in the file when there is no newline following it.
Two things to note about the while() statement.
1. The getc() assignment is embedded with the EOF test. Eliminates the repeated code.
2. If the test p < n is FALSE, it will short circuit the && and no attempt at assignment would occur. While it isn't critical the way it's done now, imagine if the statement was
Code:

`  while( (string[p] = getc(iF)) != EOF  &&  p < n )`
In main(), use the return value from getStringF() to your advantage
Code:

```while(( e = getStringF(string,max,iF)) != EOF )    {         if( !e ) continue ;         for( e--, k = 0 ; k < e && string[k] == string[e] ; )         {             ++k ;             --e ;         }         if( k >= e )             fprintf(oF,"        YES  %s*\n",string);         else             fprintf(oF,"        NO  %s*\n",string);        }```
Eliminate all that business creating a reversed string and just compare the elements themselves!