^my homework.
I don't expect you to write it. Expectations are irrelevant.
^my homework.
I don't expect you to write it. Expectations are irrelevant.
"Expectations are irrelevant."
Not a palindrome!
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?
" I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "
- Bill Gates .
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!");
Are you passing the whole string array in the function call?
Derp, I see it.
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; }
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
Figured it out.
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
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()
This will now correctly read the last word in the file when there is no newline following it.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 }
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
In main(), use the return value from getStringF() to your advantageCode:while( (string[p] = getc(iF)) != EOF && p < n )Eliminate all that business creating a reversed string and just compare the elements themselves!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); }