C 中的空开关盒

发布于 2024-12-02 00:51:46 字数 1096 浏览 1 评论 0原文

我正在阅读一些有关 C 的书籍。我发现以下在 C 中切换大小写的示例, 我试图理解......

/* caps.c */

   #include <stdio.h>
   #include <ctype.h>

   #define SEEK 0
   #define REPLACE 1

   int main(void)
   {
     int ch, state = SEEK;
     while(( ch = getchar() ) != EOF )
     {
       switch( state )
       {
       case REPLACE:
         switch( ch )
         {
         case ' ':    //leaving case empty will go to default ???
         case '\t':
         case '\n':   state = SEEK;
                      break;
         default:     ch = tolower( ch );
                      break;
         }
         break;
       case SEEK:
         switch( ch )
         {
         case ' ':
         case '\t':
         case '\n':   break;
         default:     ch = toupper( ch );
                      state = REPLACE;
                      break;
         }
       }
       putchar( ch );
     }
     return 0;
   }

对我来说很清楚,模式 SEEK,然后字母大写,然后模式设置为 REPLACE,然后字母转换为小写。但为什么空格又会触发SEEK模式呢?我的评论真的是这样吗?

I'm reading through some books about C. I found the following example to switch case in C,
which I try to understand ...

/* caps.c */

   #include <stdio.h>
   #include <ctype.h>

   #define SEEK 0
   #define REPLACE 1

   int main(void)
   {
     int ch, state = SEEK;
     while(( ch = getchar() ) != EOF )
     {
       switch( state )
       {
       case REPLACE:
         switch( ch )
         {
         case ' ':    //leaving case empty will go to default ???
         case '\t':
         case '\n':   state = SEEK;
                      break;
         default:     ch = tolower( ch );
                      break;
         }
         break;
       case SEEK:
         switch( ch )
         {
         case ' ':
         case '\t':
         case '\n':   break;
         default:     ch = toupper( ch );
                      state = REPLACE;
                      break;
         }
       }
       putchar( ch );
     }
     return 0;
   }

It is pretty clear to me that the mode SEEK, and then letters are Capitalized, and then mode is set to REPLACE, then letters are converted to lower. But why empty spaces trigger again SEEK mode ? Is this really the case in my comment ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把人绕傻吧 2024-12-09 00:51:46

这就是所谓的 C switch 运算符的失败行为。如果 case 区域末尾没有 break,控制权将传递到下一个 case 标签。

例如,以下代码片段

int x = 5;
switch( x ) {
    case 5:
        printf( "x is five!\n" );
    case 2:
        printf( "x is two!\n" );
        break;
    default:
        printf( "I don't know about x\n" );
}

输出

x is five!
x is two!

Be Care。

This is so-called fall-through behaviour of C switch operator. If you don't have a break at the end of a case region, control passes along to the next case label.

For example, the following snippet

int x = 5;
switch( x ) {
    case 5:
        printf( "x is five!\n" );
    case 2:
        printf( "x is two!\n" );
        break;
    default:
        printf( "I don't know about x\n" );
}

outputs

x is five!
x is two!

Be careful.

你不是我要的菜∠ 2024-12-09 00:51:46

在这种情况下,将会发生的情况是,开关将进入适当的 case 语句,因此如果 ch=' ' 位于顶部,则运行直到遇到 打破

这意味着如果 ch' '、'\t''\n' 之一,则 state将被设置为SEEK

In this case what will happen is that the switch will enter at the appropriate case statment, so if ch=' ' at the top then run until it hits a break.

This means that if ch is one of ' ','\t' or '\n' then state will be set to SEEK.

讽刺将军 2024-12-09 00:51:46

将一个案例留空不会进入默认状态,它会进入下一个案例。

所以这段代码:

case ' ':    // leaving case empty will drop through to `\t` and then `\n`.
case '\t':
case '\n':   state = SEEK;
             break;
default:     ch = tolower( ch );
             break;

将把空格、制表符和换行符的状态设置为SEEK,否则它将把字符小写。

这里有一个具有两个状态的小型有限状态机。

  • 在 SEEK 模式下,它将跳过所有空白(制表符、空格和换行符),并且当它找到不同的字符时,它将大写并切换到 REPLACE 模式。

  • 在REPLACE模式下,它将小写所有字符,直到找到空格,然后切换到SEEK模式。

因此,像这样的文本:

PaxDiablo is a REALLY Cool Guy

将变成:

PaxDiablo Is A Very Cool Guy

Leaving a case empty does not go to default, it drops through to the next case.

So the piece of code:

case ' ':    // leaving case empty will drop through to `\t` and then `\n`.
case '\t':
case '\n':   state = SEEK;
             break;
default:     ch = tolower( ch );
             break;

will set the state to SEEK for spaces, tabs and newlines, otherwise it will lower-case the character.

What you have here is a little finite state machine with two states.

  • In SEEK mode, it will skip over all the white space (tabs, spaces and newlines) and, when it finds a different character, it will upper-case it and switch to REPLACE mode.

  • In REPLACE mode, it will lower-case all characters until it finds white space then switch to SEEK mode.

Hence text like:

PaxDiablo is a REALLY cool guy

will become:

Paxdiablo Is A Really Cool Guy

音盲 2024-12-09 00:51:46
case ' ':    //leaving case empty will go to default ???

首先我想明确的是,
如果您编写 case ' ': ,则不会将 case 留空。您实际上正在编写 case 32: ,因为空格的 ASCII 值为 32。

一般来说,
每当您写入“a”、“b”或任何字符常量时,指定的字符都会隐式转换为其相应的 ASCII 值。

Proof: 
 The prototype of isspace() is,
 int isspace ( int c );
 And not as,
 int isspace ( char c );

注释掉 case ' ': 语句。然后你自己就会找到答案。如果我注释掉 case ' ': ,

 然后输入时,
    你好世界
    输出将是
    你好世界<不是预期的>
    而不是 
    你好世界(需要输出)。
case ' ':    //leaving case empty will go to default ???

First I want to make clear that ,
if you write case ' ': , you are not leaving the case empty. You are actually writing a case 32: , since ASCII value of space is 32.

In general,
whenever you write 'a',or'b' or any charecter constant, there will be an implicit conversion of the charecter specified to its corresponding ASCII value.

Proof: 
 The prototype of isspace() is,
 int isspace ( int c );
 And not as,
 int isspace ( char c );

Comment out the case ' ': statement. Then you will find answer yourself. If I comment out case ' ': ,

 Then on input,
    hello world
    the output will be,
    Hello world   <not expected>
    instead of 
    Hello World  (needed output).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文