【发布时间】:2015-01-06 23:25:41
【问题描述】:
我正在尝试将 C-String 转换为全小写,而不使用 ctype.h 中的 tolower。 但是我的代码似乎不起作用:我收到运行时错误。我要做的是更改大写字母 bij 'a' - 'A' 的 ASCII 值,据我所知,这应该将这些值转换为小写字母。
#include <stdio.h>
void to_lower(char* k) {
char * temp = k;
while(*temp != 0) {
if(*temp > 'A' && *temp < 'Z') {
*temp += ('a' - 'A');
}
temp++;
}
}
int main() {
char * s = "ThiS Is AN eXaMpLe";
to_lower(s);
printf("%s",s);
}
【问题讨论】:
-
您收到的错误是什么?看起来你在
to_lower之后多了一个}。 -
看起来您正在修改未定义行为的字符串文字。
-
@SimpleJ:他没有多余的
},只是未格式化的代码。 -
我收到运行时错误。
标签: c string pointers ascii tolower