通过指针在 C++ 中的 char array[] 中小写大写字母
Lowercasing Capital Letters in char array[] in C++ through Pointers
我正在尝试使用指针递归小写所有大写字母
使用 C++ 编程语言。下面是代码片段:
// Example program
#include <iostream>
#include <string>
using namespace std;
void all_lower(char* input) {
if ( *input ) {
cout << input << endl;
return;
}
if ( *input >= 'A' && *input <= 'Z') {
*input += 32; // convert capital letter to lowercase
}
cout << *input << endl;
all_lower(++input); // simply move to next char in array
}
int main() {
char test[] = "Test";
all_lower(test);
return 0;
}
最终输出为:
"Test"
尽管我试图将元素的 ASCII 码值增加 32。
您在检测到第一个非空字符时退出函数,即 'T'
,然后在退出前输出整个数组,因此您看到的是原始未修改的输入。您根本没有在数组中递归。您需要递归遍历数组,直到到达空终止符。
你需要改变这个:
if ( *input ) {
cout << input << endl;
return;
}
改为:
if ( *input == 0 ) {
return;
}
然后该函数将按预期工作。
也就是说,我建议您从函数中删除 cout
语句,并在函数退出后在 main()
中执行单个 cout
。这样会加速函数,证明test[]
数组的内容确实在被修改:
#include <iostream>
using namespace std;
void all_lower(char* input)
{
if ( *input == 0 ) {
return;
}
if ( *input >= 'A' && *input <= 'Z') {
*input += 32; // convert capital letter to lowercase
}
all_lower(++input); // simply move to next char in array
}
int main()
{
char test[] = "TEST";
cout << "Before: " << test << endl;
all_lower(test);
cout << "After: " << test << endl;
return 0;
}
而且,由于您使用的是 C++,请考虑完全删除 all_lower()
并改用 STL std::transform()
算法:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char test[] = "TEST";
cout << "Before: " << test << endl;
transform(test, test+4, test, [](char ch){ return tolower(ch); });
cout << "After: " << test << endl;
return 0;
}
简短易懂的内容:
#include <iostream>
#include <string>
using namespace std;
void all_lower(const char* input) {
if (!*input) {
std::cout << std::endl;
return;
}
std::cout << (char)(std::isalpha(*input) ? tolower(*input) : *input);
all_lower(++input); // simply move to next char in array
}
int main() {
all_lower("Test");
return 0;
}
我正在尝试使用指针递归小写所有大写字母
使用 C++ 编程语言。下面是代码片段:
// Example program
#include <iostream>
#include <string>
using namespace std;
void all_lower(char* input) {
if ( *input ) {
cout << input << endl;
return;
}
if ( *input >= 'A' && *input <= 'Z') {
*input += 32; // convert capital letter to lowercase
}
cout << *input << endl;
all_lower(++input); // simply move to next char in array
}
int main() {
char test[] = "Test";
all_lower(test);
return 0;
}
最终输出为:
"Test"
尽管我试图将元素的 ASCII 码值增加 32。
您在检测到第一个非空字符时退出函数,即 'T'
,然后在退出前输出整个数组,因此您看到的是原始未修改的输入。您根本没有在数组中递归。您需要递归遍历数组,直到到达空终止符。
你需要改变这个:
if ( *input ) {
cout << input << endl;
return;
}
改为:
if ( *input == 0 ) {
return;
}
然后该函数将按预期工作。
也就是说,我建议您从函数中删除 cout
语句,并在函数退出后在 main()
中执行单个 cout
。这样会加速函数,证明test[]
数组的内容确实在被修改:
#include <iostream>
using namespace std;
void all_lower(char* input)
{
if ( *input == 0 ) {
return;
}
if ( *input >= 'A' && *input <= 'Z') {
*input += 32; // convert capital letter to lowercase
}
all_lower(++input); // simply move to next char in array
}
int main()
{
char test[] = "TEST";
cout << "Before: " << test << endl;
all_lower(test);
cout << "After: " << test << endl;
return 0;
}
而且,由于您使用的是 C++,请考虑完全删除 all_lower()
并改用 STL std::transform()
算法:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
char test[] = "TEST";
cout << "Before: " << test << endl;
transform(test, test+4, test, [](char ch){ return tolower(ch); });
cout << "After: " << test << endl;
return 0;
}
简短易懂的内容:
#include <iostream>
#include <string>
using namespace std;
void all_lower(const char* input) {
if (!*input) {
std::cout << std::endl;
return;
}
std::cout << (char)(std::isalpha(*input) ? tolower(*input) : *input);
all_lower(++input); // simply move to next char in array
}
int main() {
all_lower("Test");
return 0;
}