![](/img/trans.png)
[英]the isupper(), islower(), toupper(), tolower() functions not working in c++
[英]C++ tolower/toupper char pointer
你们知道为什么以下代码在运行时崩溃吗?
char* word;
word = new char[20];
word = "HeLlo";
for (auto it = word; it != NULL; it++){
*it = (char) tolower(*it);
我正在尝试将char *(字符串)小写。 我正在使用Visual Studio。
谢谢
您无法将it
与NULL
进行比较。 相反,您应该将*it
与'\\0'
进行比较。 或者更好的是,使用std::string
,不必担心它:-)
总之,在遍历C样式的字符串时。 您应该循环播放,直到看到的字符为'\\0'
为止。 迭代器本身永远不会为NULL
,因为它只是指向字符串中的一个位置。 迭代器具有可以与NULL
比较的类型这一事实是您不应该直接接触的实现细节。
此外,您正在尝试写入字符串文字。 这是一个禁忌:-)。
编辑 :@Cheers和hth指出。 -Alf,如果给定负值, tolower
可能会破裂。 很遗憾,我们需要添加一个强制转换,以确保在喂入Latin-1编码的数据或类似数据时,此转换不会中断。
这应该工作:
char word[] = "HeLlo";
for (auto it = word; *it != '\0'; ++it) {
*it = tolower(static_cast<unsigned char>(*it));
}
您将word
设置为指向字符串文字,但是文字是只读的,因此当您分配给*it
时,这将导致未定义的行为。 您需要在动态分配的内存中制作一个副本。
char *word = new char[20];
strcpy(word, "HeLlo");
同样在循环中,您应该比较*it != '\\0'
。 字符串的结尾由字符表示为空字节,而不是指针为空。
给定代码(在写这篇文章时):
char* word;
word = new char[20];
word = "HeLlo";
for (auto it = word; it != NULL; it++){
*it = (char) tolower(*it);
该代码以两种不同的方式具有未定义的行为 ,如果仅文本数据稍有不同,则该代码也将以第三种方式具有UB:
缓冲区溢出。
连续条件it != NULL
直到it
在地址范围的末尾环绕指针(如果确实如此)时,才会为false
。
修改只读存储器。
指针word
设置为指向所述第一char
的字符串的文字,然后用该字符串的循环迭代和分配给每个char
。
将可能的负值传递到tolower
。
char
分类函数需要一个非负参数,或者特殊值EOF
。 在ASCII或无符号char
类型的假设下,此字符串可以很好地与字符串"HeLlo"
使用。 但是通常,例如,使用字符串"Blåbærsyltetøy"
,将每个char
值直接传递给tolower
都会导致传递负值; ch
的char
类型正确的调用是(char) tolower( (unsigned char)ch )
。
此外,代码还会发生内存泄漏 ,方法是使用new
分配一些内存,然后忘记它。
正确编码表面意图的正确方法:
using Byte = unsigned char;
auto to_lower( char const c )
-> char
{ return Byte( tolower( Byte( c ) ) ); }
// ...
string word = "Hello";
for( char& ch : word ) { ch = to_lower( ch ); }
关于如何使用以空终止的c字符串和poitners解决问题的方法,已经有了两个不错的答案。 为了完整起见,我建议您使用c ++字符串的方法:
string word; // instead of char*
//word = new char[20]; // no longuer needed: strings take care for themseves
word = "HeLlo"; // no worry about deallocating previous values: strings take care for themselves
for (auto &it : word) // use of range for, to iterate through all the string elements
it = (char) tolower(it);
它崩溃是因为您正在修改字符串文字。
有一个专用的函数供使用, strupr
使字符串大写, strlwr
使字符串小写。
这是一个用法示例:
char str[ ] = "make me upper";
printf("%s\n",strupr(str));
char str[ ] = "make me lower";
printf("%s\n",strlwr (str));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.