C 中的空开关盒
我正在阅读一些有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是所谓的 C
switch
运算符的失败行为。如果case
区域末尾没有break
,控制权将传递到下一个case
标签。例如,以下代码片段
输出
Be Care。
This is so-called fall-through behaviour of C
switch
operator. If you don't have abreak
at the end of acase
region, control passes along to the nextcase
label.For example, the following snippet
outputs
Be careful.
在这种情况下,将会发生的情况是,开关将进入适当的
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 ifch=' '
at the top then run until it hits abreak
.This means that if
ch
is one of' ','\t'
or'\n'
thenstate
will be set toSEEK
.将一个案例留空不会进入默认状态,它会进入下一个案例。
所以这段代码:
将把空格、制表符和换行符的状态设置为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:
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
首先我想明确的是,
如果您编写 case ' ': ,则不会将 case 留空。您实际上正在编写 case 32: ,因为空格的 ASCII 值为 32。
一般来说,
每当您写入“a”、“b”或任何字符常量时,指定的字符都会隐式转换为其相应的 ASCII 值。
注释掉 case ' ': 语句。然后你自己就会找到答案。如果我注释掉 case ' ': ,
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.
Comment out the case ' ': statement. Then you will find answer yourself. If I comment out case ' ': ,