#include <stdio.h>
     5
     6  #define MAX 100
     7
     8  int main(void)
     9  {
    10    char msg1[MAX], *p, *q, msg2[MAX];
    11
    12    printf("Enter a message: ");
    13    for (p=msg1; p<msg1+MAX; p++)
    14     {
    15       *p=getchar();
    16       if (*p=='\n')
    17        break;
    18      }
    19
    20    for (p--; p>=msg1; p--)
    21     {
    22      for (q=msg2; q<msg2+MAX; q++)
    23       {
    24        *q=*p;
    25       }
    26     }
    27  
    28    if (strcmp(p,q)==0) {
    29       printf("Palindrome\n");
    30      }
    31    else
    32       printf("Not a palindrome\n");
  33  
    34    return 0;
    35  }

I am trying to see if the inputted message is a palindrome. I put in a palindrome and it sayed it wasn't a palindrome. Any help would be great.

Recommended Answers

All 4 Replies

What was the word you input?

This is pretty much c not c++. You could post in the C forum, or you could use c++.

Also, please do not post line numbers in your code, the code tag adds the line numbers for you.

Defines:

#define MAX 100

should be replaced with

unsigned int MAX = 100;

(or better yet, don't use global variables at all!)

You could use an std::string:

std::string msg1;

instead of a char array:

char msg1[MAX];

You should use std::cout instead of printf.

You should use cin instead of getchar.

You should use std::string::compare instead of strcmp.

Good luck!

David

Nearly working but you have a small tiny problem with your code.

In C (and C++) your simple char strings need to be terminated with the value 0.
[That is 0x0 and not the character '0'].

You didn't do that, and you don't need it for the second loop that sets q. You
do need it for the comparison, because you might have extra characters at the
end of the comparison. You could check that with something like, printf("Length of p and q : %d %d",strlen(p),strlen(q)); .
[Note that is the C way of doing it.]

Therefore you need something like this: for lines 16/17

if (*p=='\n') 
  {
    *p=0
    break;
  }

The we come to the loops that set q, Interestingly what you have written [except for the zero terminator] actually works!!! However, you are doing this:
(a) get a letter in the word(s) in p from the back,
(b) set ALL of the array msg2, to that value
(c) leave q pointing to the first character AFTER the end of msg2.
(d) repeat from (a).

Therefore you need to fix this double loop.

Note: If you want to, you do not actually need to use a second array to copy the words into. Recall, that you can compare individual letters, e.g. if (msg[4]==msg[5]) printf("There is a double letter in this word\n");

Your logic for comparing palindrome is wrong. Palindrome ignores the placement of spaces. It only cares about the reversal arrangement of letters.

You could use the reverse string function as below:

char* ReverseString(char* oriStr)
{
	int cnt = 0, i = 0;
	int len = strlen(oriStr);
	char *revStr = new char(len);

	for(cnt = len-1, i =0; cnt >= 0; cnt--, i++)
	{
		revStr[i] = oriStr[cnt];
	}
	revStr[i] = '\0';

	return revStr;
}

With both original string and the reversed string, its now easy to compare the two strings.

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.