C语言 将小写字母转换为大写字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5500898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert small letter to capital letter
提问by yEL155
I'm trying to convert string in small letter to capital letter. I got some error (access volation) what whould do?
我正在尝试将小写字符串转换为大写字母。我遇到了一些错误(访问波动)怎么办?
int main()
{
char str[10];
int i=0;
scanf("%s", &str);
while (str[i] !=0)
{
str[i] += -32;
printf("%s", str[i]);
}
return 0;
}
thx
谢谢
回答by Sven Marnach
If you enter a string longer than 9 characters,
scanf()will try to write past the end of your string buffer.Your while-loop never terminates as you never change
i.You should use
"%c"as format string in yourprintf()call, since you are wrting characters, not null-terminated strings.
如果输入的字符串长度超过 9 个字符,
scanf()将尝试写入超过字符串缓冲区的末尾。你的 while 循环永远不会终止,因为你永远不会改变
i。您应该
"%c"在printf()调用中使用作为格式字符串,因为您正在编写字符,而不是以空字符结尾的字符串。
回答by CyberDem0n
int main()
{
char str[10];
int i=0;
scanf("%s", str);
while (str[i] != 0)
{
str[i] += -32;
i++;
}
printf("%s", str);
return 0;
}
and of course, you must check overflow of str...
当然,您必须检查 str 的溢出...
回答by apacay
As cprogrammer said
正如程序员所说
You better use toupper
/* toupper example */ #include <stdio.h> #include <ctype.h> int main () { int i=0; char str[]="Test String.\n"; char c; while (str[i]) { c=str[i]; putchar (toupper(c)); i++; } return 0; }
你最好使用toupper
/* toupper example */ #include <stdio.h> #include <ctype.h> int main () { int i=0; char str[]="Test String.\n"; char c; while (str[i]) { c=str[i]; putchar (toupper(c)); i++; } return 0; }
But if not, and you want to do it your way
但如果没有,而且你想按照自己的方式去做
int main()
{
char str[10];
int i=0;
scanf("%s", &str);
while (str[i]!='#include<stdio.h>
#include<conio.h>
#include<string.h>
void main();
{
int i, count;
char str[200];
clrscr();
printf("Enter a string");
scanf("%s", str);
count = strlen(str);
for(i=0; i<=count; i++)
{
if((str[i] >= 97) && (str[i] <= 122))
{
str[i] = str[i] - 32;
}
}
printf("%s", str);
getch();
}
' && i<10)
{// You forgot this: 'char ch = letter & 223; [letter = a-z] // Now ch is all time capital letter
' instead of 0 and also i<10
str[i] += -32;
printf("%c", str[i]);//char, not string
i++; //And this
}
return 0;
}
回答by MByD
There are few mistakes here:
这里有几个错误:
scanf("%s", &str);- since str is a pointer to char, you don't need to give its address, butscanf("%s", str);. (and as sven said, it's unsafe)while (str[i] !=0)this is an endless loop, you should incrementiat the end of the while block.str[i] += -32;will modify any char you're at, you should check if this is a lower case any time, for example:if (str[i] >= 'a' && str[i] <= 'z'){ str[i] -=32; } //couldn't format this line for some reason....
printf("%s", str[i])is again wrong way to use printf, since%sexpects tochar*, andstr[i]is achar. instead, useprintf("%c", str[i])which expects a char
scanf("%s", &str);- 因为 str 是一个指向 char 的指针,所以你不需要给出它的地址,但是scanf("%s", str);. (正如 sven 所说,这是不安全的)while (str[i] !=0)这是一个无限循环,您应该i在 while 块的末尾增加。str[i] += -32;将修改您所在的任何字符,您应该随时检查这是否是小写,例如:if (str[i] >= 'a' && str[i] <= 'z'){ str[i] -=32; } //由于某种原因无法格式化此行....
printf("%s", str[i])又是使用 printf 的错误方式,因为%s期望char*,并且str[i]是char. 相反,使用printf("%c", str[i])它需要一个字符
回答by Aneesh
/* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="Test String.\n";
char c;
while (str[i])
{
c=str[i];
putchar (toupper(c));
i++;
}
return 0;
}
回答by Arif
You can use Bitwise AND( &) operator technique to make small letter to capital letter.
您可以使用按位 AND( &) 运算符技术将小写字母转换为大写字母。

