简体   繁体   English

如何在 C 中编写从 a 到 z 和 A 到 Z 运行的单个 for 循环?

[英]How can I write a single for loop running from a to z and A to Z in C?

I want to combine both the for loops into single for loop.我想将两个 for 循环组合成一个 for 循环。 How can i do that?我怎样才能做到这一点?

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.但使用单个循环。

for (char ch = 'A' ; ch <= 'z' ; ch == 'Z' ? ch = 'a' : ++ch )
{
}

Should work -- though please, please, don't inflict this on your fellow developers.应该可以工作——尽管请,请不要把这个强加给你的开发人员。

I don't personally like this solution, but:我个人不喜欢这个解决方案,但是:

char * letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for (char * ptr = letters; *ptr != 0; ++ptr) {
    char ch = *ptr;
    ...
} 

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 */
    }
}

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';
     :
const char diff = 'a' - 'A';
for (ch = 'A' ; ch <= 'Z' ; ch++ )
{
  char small_ch = ch + diff;
 //... 
}

Try this:尝试这个:

        for (int i = 0; i < 52; ++i)
            printf("%c\n", 'A' + i + ('a' - 'Z' - 1) * (i/26));
for (int i=0;i<26;++i) {

     char ch = 'A' + i;
     // Logic for Uppercase letters.

     char ch1 = 'a' + i;
     // Logic for Lowercase letters.

}
#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();
}

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);

PS 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. PS Kobe,我在你的一个评论中看到你循环的原因是检查一个字符是否是一个字母......但循环是一种可怕的方式来做到这一点。 You could simply do你可以简单地做

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 的几个可恶方面之一。)

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.这种方法类似于 Billy 的方法,但使用了稍微不那么讨厌的循环增量语句。 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:我不介意把这个强加给其他开发人员,尽管我可能会将增量语句编写为 function 以澄清增量语句是否变得更复杂:

char nextChar(char c) { return c == 'Z' ? 'a' : c+1; }

for (char ch = 'A'; ch <= 'z'; ch = nextChar(ch)) {
       //loop body
}

It's pointless, just use one for loop.没意义,只用一个for循环。 The variables are mainly used as counters, or boundaries for the loop so it knows when to terminate.这些变量主要用作计数器或循环的边界,因此它知道何时终止。 Just make one loop.只做一个循环。 Two loops are completely unnecessary.两个循环是完全没有必要的。

void main()
{   
    char ch;

    for(ch= 65 ; ch <= 122 ; ch++)
    {
        if ( ch>=91 && ch<=96)
        continue;                     //hey check this out if this helps
        printf("%c \n",ch);
    }

}
for (ch = 'A';ch <= 'z';ch++) {

}

This will also include a few characters such as '[' and ']'...这还将包括一些字符,例如'['和']'......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM