Please help me in Palindrome Code

Check if the string is a palindrome
If a string is not a palindrome, at least how many cuts do you need to cut the string into palindromes ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include "iostream"
#include "string.h"
using namespace std;
int compare(char);

int main()
{

    char String[100];
    cin >> String;
    char_num = strlen(String);
    int i = 0, j = char_num - 1 ;
    if (char_num % 2 == 0 )
    {
        if (sossanh(String) == 1)
            cout << "OK, it is a palindrome";
        else
            cout << "The string isn't a palindrome";
    }
}
int compare(char String)
{
    int i= 0; j = char_num;
    for (;i<j;i++ && j--)
    {
        if (String[i] != String[j])
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
}
You don't ask an actual question, you're just repeating your assignment.
But let's see,

the compare function logic is off because you always return after the first iteration. Basically, you're only comparing the first and last characters. If you're trying to check if it's a palindrome, you can only return true/1 if every character meets the requirements.

sossanh
I guess this is translation, but your compare function currently only wants a char, and not a char array. change the signature of the function to
int compare(char String[])

You also never declare char_num as a variable, and you currently have it in more than one scope.
Last edited on
yes, I forgot to edit sossanh ==> compare

But I want you help me in second question
"If a string is not a palindrome, at least how many cuts do you need to cut the string into palindromes ?"
Thankyou
Topic archived. No new replies allowed.