How to Make a Palindrome?

Hello guyz, can someone teach me how to make a palindrome?
I am thinking that first i should store it in my array, then we should get the length of a string.
Then After divide it by two and compare the first and last character of the word.
Can someone help me how to code this thing?
Thanks, I'll be waiting for your help. =)
Do you want to make a palindrome or determine if a word is a palindrome?

The simplest way to determine if a word is a palindrome is to reverse the
word and compare the new word to the original.
You will have to account for ignored characters and case differences also. Before reversing, convert everything to uppercase or lowercase, and remove all characters that don't count.
I have these codes.. can someone tell me why is this an error

By the way here is my code sir.

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
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string wordenter[30];
	int length;

	cout<<"Enter a word\n";
	cin>>wordenter;
	cin.get();
	length = strlen(wordenter);
	cout<<"You enter the word "<<wordenter<<" with the length of "<<length<<endl;
	cin.get();

	//Declaration for palindrome
	int i, j, palindrome;
	for (i=0, j=length-1, palindrome=1; j>i; j--, i++)
	{
		if(toupper(wordenter[i]) != toupper(wordenter[j]))
		{
			palindrome = 0;
			break;
		}
	}
	//cout<<"Word "<<wordenter<<(palindrome?"is":"is not")<<" a palindrome."<<endl;
	printf("String %s %s a palindrome", wordenter, (palindrome ? "is" : "is not"));
	return 0;

}
This is incorrect with strings:
string wordenter[30];

and you must include header
#include <cstring>
for using function strlen
and strlen doesn't work with string but with chars.



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
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char wordenter[30];
	int length;

	cout<<"Enter a word\n";
	cin>>wordenter;
	cin.get();
	length = strlen(wordenter);
	cout<<"You enter the word "<<wordenter<<" with the length of "<<length<<endl;
	cin.get();

	int i, j, palindrome;
	for (i=0, j=length-1, palindrome=1; j>i; j--, i++)
	{
		if(toupper(wordenter[i]) != toupper(wordenter[j]))
		{
			palindrome = 0;
			break;
		}
	}
	cout<<"Word "<<wordenter<<(palindrome?"is":"is not")<<" a palindrome."<<endl;
	cin.get();
	return 0;

}
Last edited on
Got it.. thank you sir. :) i learned something today
Hi Sir
Can you please Check it for me ? i not sure


#include <iostream>
#include <string.h>

using namespace std;

int main(void){

bool palindrome=true;
string word;

cout << "Please input a word and press return" << endl;
cin>>word;
int length = strlen(word.c_str());

cout << "The word has " << length << " characters" << endl;

int char_checks=int(length/2);

cout << char_checks << " characters need to be checked" << endl;

if (length>0){
for(int i=0;i<(char_checks);i++){
cout << "checking " << word[i] << " against " << word[length-1-i] << endl;
if(word[i]!=word[length-1-i])
palindrome=false;
}
}

if(palindrome==true) cout << "The word is a palindrome" << endl;
if(palindrome==false) cout << "The word is not a palindrome" << endl;

return 0;
}
Can you please write it in source code format? :) So that i can see what is the error
So here it is.. Just found out your error..

You must change your string to char.. And if you are comparing these words.. it should be in an array form.. So here

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
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

int main(void){

bool palindrome=true;
char word[30];

cout << "Please input a word and press return" << endl;
cin>>word;
int length = strlen(word);
cin.get();
cout << "The word has " << length << " characters" << endl;

int char_checks=int(length/2);

cout << char_checks << " characters need to be checked" << endl;

if (length>0){
for(int i=0;i<(char_checks);i++)
{
cout << "checking " << word[i] << " against " << word[length-1-i] << endl;
if(word[i]!=word[length-1-i]) 
palindrome=false;
} 
}

if(palindrome==true)
{ 
cout << "The word is a palindrome" << endl; 
}

else
{
cout << "The word is not a palindrome" << endl; 
}

cin.get();
return 0;
}




Hope it helps :)
Last edited on
Topic archived. No new replies allowed.