在我编写C ++中的大写转换函数时,我注意到在C中没有得到预期的输出。
C ++函数
#include <iostream>
#include <cctype>
#include <cstdio>
void strupp(char* beg)
{
while (*beg++ = std::toupper(*beg));
}
int main(int charc, char* argv[])
{
char a[] = "foobar";
strupp(a);
printf("%s\n", a);
return 0;
}
输出结果如预期:
FOOBAR
C函数
#include <ctype.h>
#include <stdio.h>
#include <string.h>
void strupp(char* beg)
{
while (*beg++ = toupper(*beg));
}
int main(int charc, char* argv[])
{
char a[] = "foobar";
strupp(a);
printf("%s\n", a);
return 0;
}
输出结果是期望的结果,只缺少第一个字符。
OOBAR
请问有人知道在C语言编译时为什么结果会被截断吗?
std::transform(a, a + strlen(a), a, std::toupper);
。该代码会将字符串a中的所有字符转换为大写字母。 - PaulMcKenzie=
的右边在左边之前被评估? - David Schwartzstd :: transform
。 - PaulMcKenzie