C语言 如何在 C 中编写从 a 到 z 和 A 到 Z 的单个 for 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6352870/
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
How can I write a single for loop running from a to z and A to Z in C?
提问by kobe
I want to combine both the for loops into single for loop. How can i do that?
我想将两个 for 循环组合成单个 for 循环。我怎样才能做到这一点?
I want to loop through a to z, and A to Z, like so:
我想遍历 a 到 z,从 A 到 Z,如下所示:
char ch;
for (ch = 'A' ; ch <= 'Z' ; ch++ )
{
}
for (ch = 'a' ; ch <= 'z' ; ch++ )
{
}
but using a single loop.
但使用单个循环。
回答by Billy ONeal
for (char ch = 'A' ; ch <= 'z' ; ch == 'Z' ? ch = 'a' : ++ch )
{
}
Should work -- though please, please, don't inflict this on your fellow developers.
应该可以工作——尽管请不要把这强加给你的开发人员。
回答by Ernest Friedman-Hill
I don't personally likethis solution, but:
我个人不喜欢这个解决方案,但是:
char * letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (char * ptr = letters; *ptr != 0; ++ptr) {
char ch = *ptr;
...
}
回答by Ted Hopp
You can do it in a nested loop (two loops, but only one body):
您可以在嵌套循环(两个循环,但只有一个主体)中执行此操作:
for (start = 'A'; start <= 'a'; start += 'a' - 'A') {
end = start + 'Z' - 'A';
for (ch = start; ch <= end; ++ch) {
/* body */
}
}
回答by Chris Dodd
Well, the obvious question is why? ...and the second question is do you care about non-ASCII character sets (as your two loops will fail for EBCDIC), but the quick and dirty way of connecting the two is
那么,显而易见的问题是为什么?...第二个问题是您是否关心非 ASCII 字符集(因为您的两个循环将因 EBCDIC 失败),但连接两者的快速而肮脏的方法是
for (ch = 'A'; ch <= 'z'; ch++) {
if (ch > 'Z' && ch < 'a') ch = 'a';
:
回答by iammilind
const char diff = 'a' - 'A';
for (ch = 'A' ; ch <= 'Z' ; ch++ )
{
char small_ch = ch + diff;
//...
}
回答by Scott Urban
Try this:
尝试这个:
for (int i = 0; i < 52; ++i)
printf("%c\n", 'A' + i + ('a' - 'Z' - 1) * (i/26));
回答by g123
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
char e;
for(i=65;i<=122;i++)
{
if(i<91||i>96)
{
e=i;
printf("%c\n",e);
}
}
getch();
}
回答by Jim Balter
A straightforward solution is
一个简单的解决方案是
int i;
for(i = 0; i < 52; i++){
char ch = i + (i < 26? 'A' : 'a');
/* do something with ch */
}
although I prefer, especially in sensible languages that allow nested functions,
尽管我更喜欢,尤其是在允许嵌套函数的合理语言中,
for(ch = 'A'; ch <= 'Z'; ch++)
dosomething(ch);
for(ch = 'a'; ch <= 'z'; ch++)
dosomething(ch);
P.S. Kobe, I see in one of your comments that your reason for the loops is to check whether a character is a letter ... but looping is a horrid way to do that. You could simply do
PS Kobe,我在你的一个评论中看到你循环的原因是检查一个字符是否是一个字母......但循环是一种可怕的方式来做到这一点。你可以简单地做
if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')){
/* c is a letter */
}
or, considerably better,
或者,相当好,
#include ctype.h
...
if(isalpha((unsigned char)c)){
/* c is a letter */
}
(To understand why that cast is needed, read the isalpha man page and the C language standard. This is one of several abominable aspects of C.)
(要了解为什么需要这种类型转换,请阅读 isalpha 手册页和 C 语言标准。这是 C 的几个令人讨厌的方面之一。)
回答by Eamon Nerbonne
for (char ch = 'A'; ch <= 'z'; ch = ch == 'Z'?'a':ch+1) {
//loop body
}
This approach is similar to Billy's, but with a slightly less nasty loop-increment statement. I wouldn't mind inflicting this on a fellow dev, though I might write the increment statement as a function to clarify if the increment statement got any more complex:
这种方法类似于 Billy 的方法,但具有稍微不那么讨厌的循环增量语句。我不介意对开发人员造成这种影响,尽管我可能会将 increment 语句写为一个函数,以澄清 increment 语句是否变得更复杂:
char nextChar(char c) { return c == 'Z' ? 'a' : c+1; }
for (char ch = 'A'; ch <= 'z'; ch = nextChar(ch)) {
//loop body
}
回答by N.R.S.Sowrabh
for (int i=0;i<26;++i) {
char ch = 'A' + i;
// Logic for Uppercase letters.
char ch1 = 'a' + i;
// Logic for Lowercase letters.
}

