Like Tree1Likes
  • 1 Post By Adak

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

This is a discussion on Input a string from a file and tell whether that string is a palindrome. within the C Programming forums, part of the General Programming Boards category; ^my homework. I don't expect you to write it. Expectations are irrelevant....

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    31

    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.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "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?
    CommonTater likes this.

  3. #3
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by galvadon View Post
    ^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

    " 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 .

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rajarshi View Post
    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!");

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Are you passing the whole string array in the function call?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Derp, I see it.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    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;  
    }

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    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

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    Figured it out.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    26
    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

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String File Input
    By TenTierHook in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2010, 10:36 AM
  2. Reading in a string from an input file?
    By matthayzon89 in forum C Programming
    Replies: 5
    Last Post: 08-31-2010, 02:14 PM
  3. input file into a string
    By joeman in forum C++ Programming
    Replies: 6
    Last Post: 04-22-2010, 05:33 PM
  4. Recursively Checking a Palindrome string
    By TheUmer in forum C Programming
    Replies: 19
    Last Post: 12-04-2009, 06:38 PM
  5. input data from file into string
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 05-02-2006, 10:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21