2Likes 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 '
2Likes '
int getStringF(char [],int, FILE *);
main()
{
int b,e,numChar,leng,eof;
...
-
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.
-
Last edited by CommonTater; 12-09-2011 at 07:32 PM.
-
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.
-
Did you even bother to look at the link I gave you?
-
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.
-
Sorry...had a senior moment... :-\
-
Sorry "had a senior moment" is not a palindrome! 
(That's my favorite palindrome chuckle).
-
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;
}
-

Originally Posted by
Adak
Sorry "had a senior moment" is not a palindrome!
(That's my favorite palindrome chuckle).
That's ok my friend... neither is "palindrome"
-
Re: Post #18,
I'm curious if that second code is what you turned in?
-
Registered User

Originally Posted by
phantomotap Can you write code while blindfolded only with the blind covering your brain? Can you code while brainfolded?
-

Originally Posted by
whiteflags
Which doesn't work.
-

Originally Posted by
whiteflags
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
-
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.
-
@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.
Popular pages Recent additions
Similar Threads
-
By porsche911nfs in forum C++ Programming
Replies: 27
Last Post: 04-23-2009, 09:16 PM
-
By amigoloko in forum C++ Programming
Replies: 9
Last Post: 07-11-2005, 12:07 PM
-
By rrsanch in forum C++ Programming
Replies: 1
Last Post: 09-24-2004, 11:49 PM
-
By hunter_04 in forum C++ Programming
Replies: 7
Last Post: 09-20-2004, 07:38 PM
-
By hunter_04 in forum C++ Programming
Replies: 8
Last Post: 09-19-2004, 09:23 PM