Jump to content

C problems

- - - - -

  • Please log in to reply
5 replies to this topic

#1
darious

darious

    Newbie

  • Members
  • Pip
  • 1 posts
Hello Sir,
I have a problems that i am confuse
A program to find the palindrome using thsi function Pali(Sting s) S where as to check S is palin drome.
Regards
Darious.

#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
do you know what a palindrome is?
If you don't, it is words that are spelled the same front to back as back to front example:"deed"
So all you have to do is write a function that receives a string as parameter and then see if the last character is the same as the first letter and so on...
Post the code you have thus far and we will help... But we won't write the whole program for you...

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Another way to do it is to reverse the original string, then compare the two string to see if they are the same or not.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#4
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
 
BOOL Pali(char * str) {
 
 int l=strlen(str);
 int h=l/2;
 int i;
 for (i=0; i<h; i++) {
  if (str[ i]!=str[l-i-1]) return 0; //the space after the first [ is so this isn't turned into italics... lol
}
return 1;
}

I think... but I'm not sure. If it works.. happy happy!

Anyway, the point is that you only have to compare half of the string... the left half to the right half, mirrored. After all, if the first half is the mirror of the second half, then obviously, the second half is the mirror of the first. If an uneven number of chars in the string, then the middle is the same as itself (duh! This is called "identity", AA) and doesn't need to be checked.

Edited by Firebird_38, 21 April 2010 - 05:39 PM.
Fixed according to lintwurm's pwer specs

Posted Image


#5
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
Just so you know, the for loop in your function is wrong... ^_^

for (i=0; i++; i<h) is what you have, but should be: for (i=0; i<h ; i++)
and then the program runs smoothly ^_^

#6
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
lo. That's right. I'm not that good at C, because I don't really like it. But I shoulda done that one right, as in for x=start to finish step stepsize, same order. But Pascal has no "step", basic does (at least it did when I still used it 10 years ago). I normally use Delphi, and am not too well versed in C, since I can do all that with Delphi anyway.
But I guess this is the wrong place to say that I prefer Delphi :).

Posted Image





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users