反转字符数组
Reversing char array
当我打印出 text2 时,我看到它绝对不是我给它的字符串的反转,我不确定为什么会这样。当我输入 "test" 时,我会得到类似“Ş\2200+”的东西。我可以在 char 数组上使用 strncpy 吗?也许它需要用一个循环来完成 - 不确定。任何帮助,将不胜感激。 :)
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char text[79], text2[79];
bool input = true;
while (input) {
cout << "Please give me a line of text to examine: ";
cin.getline(text, 79);
for(int i = 0; i < strlen(text); i++ )
cout << text[i];
// test to see if it is a palindrome
strncpy(text, text2, 80);
reverse(text2, text2 + strlen(text2));
printf("%s", text2); `// when I print this out I get something odd`
if (strcmp(text, text2) == 0)
cout << " is a palindrome!" << endl;
else
cout << " is not a palindrome." << endl;
if (strcmp(text, "END") == 0)
input = false;
else
cout << "\ntype END to exit the program" << endl;
} // end while loop
} // end main
您似乎以错误的方式使用了 strncpy
:您可能想将 text
复制到 text2
,而不是相反。
有一个更简单的方法来测试一个字符串是否是回文,即:
bool is_palindrome(const char* s, size_t n) {
size_t i, j;
i = 0, j = n-1;
while (i < j && s[i++] == s[j--])
;
return i >= j;
}
通常您会看到 char*
的这种反转技术:
void reverse(char* s) {
if(!s) return;
size_t n = strlen(s);
for(size_t i = 0; i < n/2; ++i) {
char tmp = s[i];
s[i] = s[n - i - 1];
s[n - i - 1] = tmp;
}
}
但是,这不适用于非 ASCII 字符。原因是非ASCII字符需要多个字节来表示。
您将需要使用 wide characters 来处理多字节代码点,但逻辑应遵循上述。
为什么不使用 <algorithm>
中的 std::vector<char>
和 std::reverse
来解决您的问题?
我会做类似下面的事情:
(请注意,我使用的是 C++11 基于范围的 for 循环和 auto
,您可以将其更改为常规 for 循环并使用std::string line
如果你没有支持这个的编译器)。
int main()
{
cout << "Please give me a line of text to examine: ";
auto line = ""s;
getline(cin, line);
// Push back every character to the vector
vector<char> vtext;
for (const auto &elem : line)
vtext.push_back(elem);
// Create a copy of the vector<char> and reverse the copy
vector<char> vtext_reversed{vtext};
reverse(begin(vtext_reversed), end(vtext_reversed));
// Print the line reversed
cout << "\nThis is the line reversed: ";
for (const auto &elem : vtext_reversed)
cout << elem;
}
当我打印出 text2 时,我看到它绝对不是我给它的字符串的反转,我不确定为什么会这样。当我输入 "test" 时,我会得到类似“Ş\2200+”的东西。我可以在 char 数组上使用 strncpy 吗?也许它需要用一个循环来完成 - 不确定。任何帮助,将不胜感激。 :)
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char text[79], text2[79];
bool input = true;
while (input) {
cout << "Please give me a line of text to examine: ";
cin.getline(text, 79);
for(int i = 0; i < strlen(text); i++ )
cout << text[i];
// test to see if it is a palindrome
strncpy(text, text2, 80);
reverse(text2, text2 + strlen(text2));
printf("%s", text2); `// when I print this out I get something odd`
if (strcmp(text, text2) == 0)
cout << " is a palindrome!" << endl;
else
cout << " is not a palindrome." << endl;
if (strcmp(text, "END") == 0)
input = false;
else
cout << "\ntype END to exit the program" << endl;
} // end while loop
} // end main
您似乎以错误的方式使用了 strncpy
:您可能想将 text
复制到 text2
,而不是相反。
有一个更简单的方法来测试一个字符串是否是回文,即:
bool is_palindrome(const char* s, size_t n) {
size_t i, j;
i = 0, j = n-1;
while (i < j && s[i++] == s[j--])
;
return i >= j;
}
通常您会看到 char*
的这种反转技术:
void reverse(char* s) {
if(!s) return;
size_t n = strlen(s);
for(size_t i = 0; i < n/2; ++i) {
char tmp = s[i];
s[i] = s[n - i - 1];
s[n - i - 1] = tmp;
}
}
但是,这不适用于非 ASCII 字符。原因是非ASCII字符需要多个字节来表示。
您将需要使用 wide characters 来处理多字节代码点,但逻辑应遵循上述。
为什么不使用 <algorithm>
中的 std::vector<char>
和 std::reverse
来解决您的问题?
我会做类似下面的事情:
(请注意,我使用的是 C++11 基于范围的 for 循环和 auto
,您可以将其更改为常规 for 循环并使用std::string line
如果你没有支持这个的编译器)。
int main()
{
cout << "Please give me a line of text to examine: ";
auto line = ""s;
getline(cin, line);
// Push back every character to the vector
vector<char> vtext;
for (const auto &elem : line)
vtext.push_back(elem);
// Create a copy of the vector<char> and reverse the copy
vector<char> vtext_reversed{vtext};
reverse(begin(vtext_reversed), end(vtext_reversed));
// Print the line reversed
cout << "\nThis is the line reversed: ";
for (const auto &elem : vtext_reversed)
cout << elem;
}