Hi everyone,

I was browsing through the forum to find some code for a palindrome program. I get some parts of it, but I'm confused what a certain while loop does. Can anyone help clarify?

char s[256],t[256]; /*char array of length 256*/
  char *p,*q,*r; /*define char array*/
  int i=0,j=0; /*initialize int i and j*/
  printf("Please enter a word.\n"); /*print prompt*/
  scanf("%s",s); /*read in string from the user*/
p=s+strlen(s)-1;
  r=p;
  while(i<strlen(s)){ 
    t[i]=*p;
    p--;
    i++;}
    q=s;
  while(j<strlen(s)){
    if(*r==*q){
    r--;
    q++;
    j++;}
    else {
      break;
    }
    }

I put in some comments, but I would appreciate if anyone could point out errors in my understanding.

Thanks

Whomever wrote this code was a little confused himself. I suggest you come up with your own solution.

p, q, and r are pointers to characters, not character arrays.

The first while loop reverses the string in s[] and puts the result in t[] (but it forgets the null terminator).

The second loop starts out with r pointing to the last character in s[] and q pointing at the first character in s[]. The loop then increments q and decrements r so long as the characters they point to are equal. If the string in s[] is a character-by-character palindrome (including spaces, punctuation, etc.) then r will be equal to s-1 when the loop terminates. You'll notice that t[] is completely forgotten...

Again, forget this code and come up with your own solution. The idea of copying only characters that we want to consider (such as A-Z, 0-9, etc.) from the string the user input to another string is a good idea.

Another good idea is starting a character pointer (q) at the beginning of the string containing only valid characters and one (r) at the end and doing like in loop 2, but ending when r < q.

Finally, use better variable names than s, q, r, etc.

Good luck.

Hi everyone,

I was browsing through the forum to find some code for a palindrome program. I get some parts of it, but I'm confused what a certain while loop does. Can anyone help clarify?

char s[256],t[256]; /*char array of length 256*/
  char *p,*q,*r; /*define char array*/
  int i=0,j=0; /*initialize int i and j*/
  printf("Please enter a word.\n"); /*print prompt*/
  scanf("%s",s); /*read in string from the user*/
p=s+strlen(s)-1;
  r=p;
  while(i<strlen(s)){ 
    t[i]=*p;
    p--;
    i++;}
    q=s;
  while(j<strlen(s)){
    if(*r==*q){
    r--;
    q++;
    j++;}
    else {
      break;
    }
    }

I put in some comments, but I would appreciate if anyone could point out errors in my understanding.

Thanks

You and me both. The second while loop is the hard way. It's much easier to do t=0; and then just do a strcmp(s,t); after the first while loop!

Hoppy

You should first under the algorthirm of finding a palindrome, then read the code, basically you need to compare str and str[n-i-1] to find out whether they are equal or not.

Hi everyone,

I was browsing through the forum to find some code for a palindrome program. I get some parts of it, but I'm confused what a certain while loop does. Can anyone help clarify?

char s[256],t[256]; /*char array of length 256*/
  char *p,*q,*r; /*define char array*/
  int i=0,j=0; /*initialize int i and j*/
  printf("Please enter a word.\n"); /*print prompt*/
  scanf("%s",s); /*read in string from the user*/
p=s+strlen(s)-1;
  r=p;
  while(i<strlen(s)){ 
    t[i]=*p;
    p--;
    i++;}
    q=s;
  while(j<strlen(s)){
    if(*r==*q){
    r--;
    q++;
    j++;}
    else {
      break;
    }
    }

I put in some comments, but I would appreciate if anyone could point out errors in my understanding.

Thanks

for(i=0,j=strlen(s)-1;i<j;i++,j--)
{
if(s!=s[j])
break;
}
if(i>=j)
printf("String a palindrome ");
else
printf("String not a palindrome ");
getch();

Found amusing that you did not feel the need to define the main function nor to declare the variables used in the for loop, but it was absolutely necesary to make a call to a no-portable function like getch().

Read here, for next time, how to post code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.