[英]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;
}
在這里,boolean 數組的大小為 (length+1)/2,
對於像abcba
這樣的字符串,它的長度為 3。
這對應於aa
、 bb
和c c
之間的比較。 由於中間元素相同,因此該情況下的條件始終為真。
此外,去除了中間的概念,並要求指針移動,直到它們相互交叉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.