【发布时间】:2015-04-22 11:35:13
【问题描述】:
输出与输入相同,我在哪里犯了错误? 请检查测试版本,它打印'A'的ASCII码而不是A为什么会这样?
循环中的第一个 if 条件是确保字符串仅以有效字符而不是空格开头。
void caps(char* p)
{
char* begin=NULL;
char* temp=p;
while(*temp)
{
if((begin == NULL) && (*temp!= ' '))
begin=temp;
if(begin && ((*(temp+1) == ' ' ) || (*(temp+1)=='\0')))
{
toupper(*temp);
begin=NULL;
}
temp++;
}
cout<<p;
}
int main()
{
char str[]={"i like programming"};
cout<< str <<endl;
caps(str);
return 0;
}
测试
如果我使用 printf("%c",toupper(a)) 它会正确打印 'A'。
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char a= 'a';
cout<<toupper(a); //prints ASCII code of A(65) but why not 'A' ?
return 0;
}
【问题讨论】:
-
从上面的链接:它被故意返回为
int,您可以简单地将toupper的返回转换为char,它会按您的预期工作。 -
好的,它适用于第二种情况,但我如何使它适用于第一种情况?
-
你在做
toupper(*temp);你应该在做*temp = toupper(*temp);,因为toupper函数不使用引用,因此不能自动更改*temp。 -
另外,这个问题的标题表明你需要第一个字符大写。您的代码看起来正在更改单词的最后一个字符。您应该在
begin = temp的同一位置转换为大写。 -
代码中存在错误,它确实更改了单词的最后一个字符。我正在努力解决这个问题