【发布时间】:2014-08-11 21:52:25
【问题描述】:
不知道为什么输入的字符串中的大写字母会变成随机码
#include<stdio.h>
#include<string.h>
#define length 100
int main()
{
int count;
char word[length];
printf("Please input your word =");
scanf("%s", &word);
count=strlen(word);
int i;
for(i=0;i<=count;i++)
{
if(word[i]>=97||word[i]<=122)
{
word[i]+= -32;
}
printf(" %c",word[i]);
}
return 0;
}
【问题讨论】:
-
请使用 toupper - linux.die.net/man/3/toupper
-
在 "if(word[i]>=97||word[i]
-
如果您将其标记为 C++,为什么您的代码是“C”?在 C++ 中将单词转换为大写是单行算法调用。其次,你的循环有一个错误——它的
i上升到<=count。因此,您在不应该的时候使用空终止符。 -
改用
word[i] >= 'a' && word[i] <= 'z'
标签: c++ c uppercase capitalize