Like Tree2Likes

Palindrome Program Help!

This is a discussion on Palindrome Program Help! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #define MAX 50 #define Null '

Like Tree2Likes

Thread: Palindrome Program Help!

' int getStringF(char [],int, FILE *); main() { int b,e,numChar,leng,eof; ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    Palindrome Program Help!

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX 50
    #define Null '\0'
    int getStringF(char [],int, FILE *);
    main()
    {
     int b,e,numChar,leng,eof;
     float comp;
     char pal[MAX+1], rpal[MAX+1],temp;
     FILE *payInFile, *payOutFile;
     payInFile = fopen("sample.txt","r");
     payOutFile = fopen("sampout.txt","w");
     fprintf(payOutFile,"Palindrome Program Fall 2011 by Bradley Lantz\n");
     fprintf(payOutFile,"Project #6 Due 12/9/11\n\n");
     fprintf(payOutFile,"Palindrome?    Input String\n");
     numChar = getStringF(pal,MAX,payInFile); //printf here prints 1st string
     leng = numChar;
     for(b=0;b<MAX;b++)
          rpal[b]=0;
     while (numChar!=0)
     {
           temp=0;
           e = leng;
        for (b=0;b!=Null; b++) //b<numChar && e!=0 (condition)
      {
             pal[b] = rpal[leng]; //pal[e-1]
             leng--;
            }
           printf("%s %s", pal, rpal);
        comp = strcmp(pal,rpal);
        if (comp==0)
      fprintf(payOutFile,"YES   %s*\n",pal);
        else
      fprintf(payOutFile,"NO    %s*\n",pal);
        numChar = getStringF(pal,MAX,payInFile); //printf here prints 1st string
        leng = numChar;
     }
     fprintf(payOutFile,"\nEnd of Program, Normal Termination");
     system("pause");
    }
    int getStringF(char s1[],int max, FILE *in)
    {
     char c;
     int p=0;
     c=getc(in);
     while ((c!=EOF) && (c!='\n') && (p < max))
     {
        s1[p]=c;
        p++;
        c=getc(in);
     }
     s1[p] = '\0';
     return p;
    }
    I can't get my for loop to reverse the array pal into rpal (reverse palindrome). I've tried putting in a temp. I've tried changing the order. Nothing I do works. I just got it to the point now where it'll print out the string properly I just need it to check whether or not the string is a palindrome or not.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Last edited by CommonTater; 12-09-2011 at 07:32 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    That's the thing. The program is supposed to take a line from a file and determine whether or not it's a palindrome. Everything works besides the for loop, and I don't know how to make it work. If I can get that to work then everything else should work, because that's the only problem.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you even bother to look at the link I gave you?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to STOP writing code, and think about how you determine if a string is a palindrome or not.

    You don't need to go all the way to the end of the string!

    You need a pointer (or index) at the base of the array (call it left), and you need another one on the end of the string (call it right). While left==right char's, you increment left, and decrement right. When they pass each other in the middle, or left doesn't equal right anymore, the loop should end.

    You'll need to handle spaces so you "skip" over them, for both left and right, independently.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    Sorry...had a senior moment... :-\

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sorry "had a senior moment" is not a palindrome!

    (That's my favorite palindrome chuckle).

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    This was my last project for my first computer programming class, which I turned in today! I like computer science! I learned how to js inject this morning! I deleted the White House!!!!!!!!!!!!!!!!!

    Terminal Velocity of a Cow
    Code:
    #include<stdio.h>
    #include<math.h>
    main(){
     float tV,m,g=9.81,cD=0.1,p=1.2754,a,maths;
     printf("How fat is your cow in kilograms?\n");
     scanf("%f",&m);
     printf("How many cubic kilometers does your cow take up?\n");
     scanf("%f",&a);
     maths=((2*m)*g)/((p*a)*cD);
     tV=sqrt(maths);
     printf("At IUPAC standard temperature and pressure and assuming your cow is a\n smooth sphere, the terminal velocity of your cow is: %.2f meters per second.",tV);
     system("pause");
    }
    Palindrome ++
    Code:
    #include<stdio.h>
    #include<string.h>
    int max=50;
    int getStringF(char[],int,FILE *);
    main()
    {
     FILE *oF,*iF;
     oF=fopen("output.txt","w");
     iF=fopen("data6.txt","r");
     fprintf(oF,"Palindrome program Fall 2011 \n");
     fprintf(oF,"Project #6 Due 12/9/11 rundate\n\n");
     fprintf(oF,"Palindrome?  Input string Name: Good Ole Rick. He's a cowboy.\n");
     char string[max];
     int e;
     e=getStringF(string,max,iF);
     while(e!=-1)
     {
      char string2[max]; 
      int i=0,j=0;
      for(i=strlen(string)-1;i>=0;i--)
      { 
       string2[j]=string[i];
       j++;
      }
      string2[j]='\0';
      if((strcmp(string,string2))==0)
       fprintf(oF,"        YES  %s*\n",string);
      else
       fprintf(oF,"         NO  %s*\n",string);
      e=getStringF(string,max,iF);
     }
     fprintf(oF,"\n\n\nEnd of program, Normal Termination");
     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;  
    }

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Sorry "had a senior moment" is not a palindrome!

    (That's my favorite palindrome chuckle).
    That's ok my friend... neither is "palindrome"

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Re: Post #18,

    I'm curious if that second code is what you turned in?

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Quote Originally Posted by galvadon View Post
    This was my last project for my first computer programming class, which I turned in today! I like computer science! I learned how to js inject this morning! I deleted the White House!!!!!!!!!!!!!!!!!
    And now you're giving people their entire homework projects!!!!!!!!!!!!!!


  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by whiteflags View Post
    And now you're giving people their entire homework projects!!!!!!!!!!!!!!
    Which doesn't work.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    And now you're giving people their entire homework projects!!!!!!!!!!!!!!

    Not to worry... both programs have problems --like in the second one string2 is only valid from line 18 to 24-- so it's unlikely anyone will want to copy them

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Sigh... Why do I feel like such a square for indenting and commenting code... Is it cool to not indent or comment? Must have missed the memo.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    31
    @commontater

    Explain. "Valid"? I've taken formal logic and I'm guessing it doesn't mean the same thing. I think you mean that I didn't have to add the null character, which I thought about but was tired and wanted to go to bed. But I guess there's always improvement no matter how perfect you think you are.

    also@whiteflags

    Well, he pretty much had the whole thing already, and "giving it to him" is easily equated with "read from the book/self-taught/raising your hand and asking the teacher". I guess you could make the case that he would learn more on his own, but I think that's just a romantic myth of independence that misunderstands how human beings work -- I recommend reading from Saussure to Lacan before basing your anger on sweeping assumptions about the human condition you probably aren't aware that you're making. Edit: Sweeping assumptions made in the folk-privacy of one's mind that don't consider how many years of epistemological study have gone into how annoyingly coercive "common sense" is.
    Last edited by galvadon; 12-10-2011 at 01:09 PM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome-kinda program with pointers
    By porsche911nfs in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2009, 09:16 PM
  2. Need help , Palindrome program
    By amigoloko in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2005, 12:07 PM
  3. Palindrome Program using Memory mapping
    By rrsanch in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2004, 11:49 PM
  4. Palindrome program help please
    By hunter_04 in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2004, 07:38 PM
  5. C++ Palindrome program help
    By hunter_04 in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 09:23 PM

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