判断一个单词是否为回文的程序
A program to find out if a word is palindrome
写了一些算法来找出给定的单词是否是回文。但是当我调试时,我的一个变量 (counter
) 似乎没有更新,我无法弄清楚它有什么问题。不过我可能是错的...需要任何帮助,因为我不想盲目地在线复制一些代码。
下面是代码:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
//take input
string input;
cout << "Enter your word: ";
cin >> input;
//initialize arrays and variables
int counter = 0, k = 0;
int char_length = input.length();
char characters[char_length];
strcpy(characters, input.c_str());//copy the string into char array
//index of character at the midpoint of the character array
int middle = (char_length-1)/2;
int booleans[middle]; //to keep 1's and 0's
//check the characters
int m = 0, n = char_length-1;
while(m < middle && n > middle){
if(characters[m] == characters[n]){
booleans[k] = 1;
} else {
booleans[k] = 0;
}
k++;
m++;
n--;
}
//count number of 1's (true for being equal) in the booleans array
for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0])-1; i++){
counter += booleans[i];
}
//compare 1's with size of array
if(counter == middle){
cout << input << " is a Palindrome!" << endl;
} else {
cout << input << " is not a Palindrome!" << endl;
}
return 0;
}
兄弟,你的问题是什么,你输入的是什么代码,好像很难理解。我不是很有经验,但根据我的说法,回文是一个非常非常简单和容易的程序,我会把它写成:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[20], str2[20];
int i, j, len = 0, flag = 0;
cout << "Enter the string : ";
gets(str1);
len = strlen(str1) - 1;
for (i = len, j = 0; i >= 0 ; i--, j++)
str2[j] = str1[i];
if (strcmp(str1, str2))
flag = 1;
if (flag == 1)
cout << str1 << " is not a palindrome";
else
cout << str1 << " is a palindrome";
return 0;
}
它在您可以尝试的所有情况下都有效。
如果你得到不匹配,即 (characters[m] == characters[n])
为假,那么你就没有回文。您可以在此时中断循环,返回 false 作为结果。您不这样做,而是在结果已知时继续进行测试。我会做类似的事情:
// Check the characters.
int lo = 0;
int hi = char_length - 1;
int result = true; // Prefer "true" to 1 for better readability.
while (lo < hi) { // Loop terminates when lo and hi meet or cross.
if(characters[lo] != characters[hi]) {
// Mismatched characters so not a palindrome.
result = false;
break;
}
lo++;
hi--;
}
我做了一些文体上的改进并清理了逻辑。您为解决问题做的工作太多了。
顺便说一句,当lo
和hi
两个指针相等时,你不需要检查,因为它们都指向一个奇数的单词的中间字符字母。由于该字符必须等于其自身,因此无需测试。因此循环条件中的 <
而不是 <=
.
现有代码不适用于奇数长度的回文,因为
for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0])-1; i++)
使用 i<=sizeof(booleans)/sizeof(booleans[0])-1;
或 i<sizeof(booleans)/sizeof(booleans[0]);
。
目前,您不计算 character[middle-1]
和 character[middle+1]
的比较。
对于偶数长度的回文,你将不得不稍微改变你的逻辑,因为偶数长度的回文没有定义的中点。
#include <iostream>
#include <cstring>
using namespace std;
int main(){
//take input
string input;
cout << "Enter your word: ";
cin >> input;
//initialize arrays and variables
int counter = 0, k = 0;
int char_length = input.length();
char characters[char_length];
strcpy(characters, input.c_str());//copy the string into char array
//index of character at the midpoint of the character array
int middle = (char_length+1)/2;
int booleans[middle]; //to keep 1's and 0's
//check the characters
int m = 0, n = char_length-1;
while(m<=n){
if(characters[m] == characters[n]){
booleans[k] = 1;
} else {
booleans[k] = 0;
}
k++;
m++;
n--;
}
//count number of 1's (true for being equal) in the booleans array
for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0]); i++){
counter += booleans[i];
}
cout<<counter<<" "<<middle<<endl;
//compare 1's with size of array
if(counter == middle){
cout << input << " is a Palindrome!" << endl;
} else {
cout << input << " is not a Palindrome!" << endl;
}
return 0;
}
这里布尔数组的大小是(length+1)/2,
对于像abcba
这样的字符串,它的长度是3。
这对应于a a
、b b
和c c
之间的比较。由于中间元素相同,因此条件始终为真。
此外,去除了中的概念,要求指针移动直到彼此交叉。
写了一些算法来找出给定的单词是否是回文。但是当我调试时,我的一个变量 (counter
) 似乎没有更新,我无法弄清楚它有什么问题。不过我可能是错的...需要任何帮助,因为我不想盲目地在线复制一些代码。
下面是代码:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
//take input
string input;
cout << "Enter your word: ";
cin >> input;
//initialize arrays and variables
int counter = 0, k = 0;
int char_length = input.length();
char characters[char_length];
strcpy(characters, input.c_str());//copy the string into char array
//index of character at the midpoint of the character array
int middle = (char_length-1)/2;
int booleans[middle]; //to keep 1's and 0's
//check the characters
int m = 0, n = char_length-1;
while(m < middle && n > middle){
if(characters[m] == characters[n]){
booleans[k] = 1;
} else {
booleans[k] = 0;
}
k++;
m++;
n--;
}
//count number of 1's (true for being equal) in the booleans array
for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0])-1; i++){
counter += booleans[i];
}
//compare 1's with size of array
if(counter == middle){
cout << input << " is a Palindrome!" << endl;
} else {
cout << input << " is not a Palindrome!" << endl;
}
return 0;
}
兄弟,你的问题是什么,你输入的是什么代码,好像很难理解。我不是很有经验,但根据我的说法,回文是一个非常非常简单和容易的程序,我会把它写成:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[20], str2[20];
int i, j, len = 0, flag = 0;
cout << "Enter the string : ";
gets(str1);
len = strlen(str1) - 1;
for (i = len, j = 0; i >= 0 ; i--, j++)
str2[j] = str1[i];
if (strcmp(str1, str2))
flag = 1;
if (flag == 1)
cout << str1 << " is not a palindrome";
else
cout << str1 << " is a palindrome";
return 0;
}
它在您可以尝试的所有情况下都有效。
如果你得到不匹配,即 (characters[m] == characters[n])
为假,那么你就没有回文。您可以在此时中断循环,返回 false 作为结果。您不这样做,而是在结果已知时继续进行测试。我会做类似的事情:
// Check the characters.
int lo = 0;
int hi = char_length - 1;
int result = true; // Prefer "true" to 1 for better readability.
while (lo < hi) { // Loop terminates when lo and hi meet or cross.
if(characters[lo] != characters[hi]) {
// Mismatched characters so not a palindrome.
result = false;
break;
}
lo++;
hi--;
}
我做了一些文体上的改进并清理了逻辑。您为解决问题做的工作太多了。
顺便说一句,当lo
和hi
两个指针相等时,你不需要检查,因为它们都指向一个奇数的单词的中间字符字母。由于该字符必须等于其自身,因此无需测试。因此循环条件中的 <
而不是 <=
.
现有代码不适用于奇数长度的回文,因为
for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0])-1; i++)
使用 i<=sizeof(booleans)/sizeof(booleans[0])-1;
或 i<sizeof(booleans)/sizeof(booleans[0]);
。
目前,您不计算 character[middle-1]
和 character[middle+1]
的比较。
对于偶数长度的回文,你将不得不稍微改变你的逻辑,因为偶数长度的回文没有定义的中点。
#include <iostream>
#include <cstring>
using namespace std;
int main(){
//take input
string input;
cout << "Enter your word: ";
cin >> input;
//initialize arrays and variables
int counter = 0, k = 0;
int char_length = input.length();
char characters[char_length];
strcpy(characters, input.c_str());//copy the string into char array
//index of character at the midpoint of the character array
int middle = (char_length+1)/2;
int booleans[middle]; //to keep 1's and 0's
//check the characters
int m = 0, n = char_length-1;
while(m<=n){
if(characters[m] == characters[n]){
booleans[k] = 1;
} else {
booleans[k] = 0;
}
k++;
m++;
n--;
}
//count number of 1's (true for being equal) in the booleans array
for(int i = 0; i < sizeof(booleans)/sizeof(booleans[0]); i++){
counter += booleans[i];
}
cout<<counter<<" "<<middle<<endl;
//compare 1's with size of array
if(counter == middle){
cout << input << " is a Palindrome!" << endl;
} else {
cout << input << " is not a Palindrome!" << endl;
}
return 0;
}
这里布尔数组的大小是(length+1)/2,
对于像abcba
这样的字符串,它的长度是3。
这对应于a a
、b b
和c c
之间的比较。由于中间元素相同,因此条件始终为真。
此外,去除了中的概念,要求指针移动直到彼此交叉。