I'm pretty sure this is a rookie mistake, but I can't figure it out. I tried making a palindrome checker, but whenever I try to split a passed char array in half it adds random (?) characters at the end of the split char arrays. I tried googling but to no avail. Perhaps it has something to do with a null terminated string, but the length of the char array indicates that there are no other characters other than the ones I passed. Code here:
我很确定这是一个新手的错误,但我无法弄清楚。我尝试制作回文检查器,但每当我尝试将传递的char数组分成两半时,它会在split char数组的末尾添加随机(?)字符。我试过谷歌搜索但无济于事。也许它与空终止字符串有关,但char数组的长度表示除了我传递的字符之外没有其他字符。代码在这里:
#include <iostream>
bool isPalindrome(char* passedString){
int len = strlen(passedString);
char lh[len/2];
char rh[len/2];
std::cout << "Length is: " << len << std::endl;
if(len % 2 == 0){
for(int i = 0; i < len/2; i++){
lh[i] = passedString[i];
}
int tmpCount = 0;
for(int i = len/2; i < len; i++){
rh[tmpCount] = passedString[i];
tmpCount++;
}
}
std::cout << "Left half: " << lh << std::endl << "Right half: " << rh << std::endl;
return 0;
}
int main(){
isPalindrome("test");
return 0;
}
Returns this output after compiling and executing:
编译并执行后返回此输出:
Length is: 4
Left half: te�Y�
Right half: st�Y�
2 个解决方案
#1
Make the two arrays of size len/2+1
and add \0
at the last position of each array. For more insight as to how strings work with arrays and pointers, you can check this link: http://www.cs.bu.edu/teaching/c/string/intro/
制作两个大小为len / 2 + 1的数组,并在每个数组的最后位置添加\ 0。有关字符串如何与数组和指针一起使用的更多信息,可以查看以下链接:http://www.cs.bu.edu/teaching/c/string/intro/
#2
First of all string literals have types of constant character arrays. So if you want to call the function passing to it a string literal it has to be declared like
首先,字符串文字具有常量字符数组的类型。因此,如果你想调用传递给它的函数一个字符串文字,它必须被声明为
bool isPalindrome( const char *passedString );
Secondly there is no variable length arrays in C++ (though some compilers can support them). So these declarations
其次,C ++中没有可变长度数组(尽管有些编译器可以支持它们)。所以这些声明
char lh[len/2];
char rh[len/2];
are not valid C++ declarations.
不是有效的C ++声明。
The both character arrays do not contain zero terminated strings. So this statement
两个字符数组都不包含零终止字符串。所以这句话
std::cout << "Left half: " << lh << std::endl << "Right half: " << rh << std::endl;
can output some garbage.
可以输出一些垃圾。
Instead of std::cout << lh
or std::cout << rh
use
而不是std :: cout << lh或std :: cout << rh使用
std::cout.write( lh, len / 2 );
and
std::cout.write( rh, len / 2 );
For example
std::cout << "Left half: ";
std::cout.write( lh, len / 2 );
std::cout << std::endl << "Right half: ";
std::cout.write( rh, len / 2 ) << std::endl;
#1
Make the two arrays of size len/2+1
and add \0
at the last position of each array. For more insight as to how strings work with arrays and pointers, you can check this link: http://www.cs.bu.edu/teaching/c/string/intro/
制作两个大小为len / 2 + 1的数组,并在每个数组的最后位置添加\ 0。有关字符串如何与数组和指针一起使用的更多信息,可以查看以下链接:http://www.cs.bu.edu/teaching/c/string/intro/
#2
First of all string literals have types of constant character arrays. So if you want to call the function passing to it a string literal it has to be declared like
首先,字符串文字具有常量字符数组的类型。因此,如果你想调用传递给它的函数一个字符串文字,它必须被声明为
bool isPalindrome( const char *passedString );
Secondly there is no variable length arrays in C++ (though some compilers can support them). So these declarations
其次,C ++中没有可变长度数组(尽管有些编译器可以支持它们)。所以这些声明
char lh[len/2];
char rh[len/2];
are not valid C++ declarations.
不是有效的C ++声明。
The both character arrays do not contain zero terminated strings. So this statement
两个字符数组都不包含零终止字符串。所以这句话
std::cout << "Left half: " << lh << std::endl << "Right half: " << rh << std::endl;
can output some garbage.
可以输出一些垃圾。
Instead of std::cout << lh
or std::cout << rh
use
而不是std :: cout << lh或std :: cout << rh使用
std::cout.write( lh, len / 2 );
and
std::cout.write( rh, len / 2 );
For example
std::cout << "Left half: ";
std::cout.write( lh, len / 2 );
std::cout << std::endl << "Right half: ";
std::cout.write( rh, len / 2 ) << std::endl;