为什么这样做? (rot13功能) [英] Why does this work? (rot13 function)
问题描述
这是我写的一个小功能,灵感来自线程
紧急帮助!需要Caesar Cipher PLEASE
$ cat /home/keisar/bin/c/ymse/rot13.h
char rot13(char字符)
{
int change;
changed = character - ''a''+''n'';
返回已更改;
}
我发现此代码有两个奇怪之处:<br />
1)我不知道不必指定b应该被替换为n,
c by o等等。怎么样?
2)该函数返回一个char(char rot13),但是改变了
是一个整数。怎么可能?
This is a little function I wrote, inspired by the thread
"Urgent HELP! required for Caesar Cipher PLEASE"
$ cat /home/keisar/bin/c/ymse/rot13.h
char rot13(char character)
{
int changed;
changed = character - ''a'' + ''n'';
return changed;
}
I find two things strange about this code:
1) I don''t have to specify that b should be replaced by n,
c by o and so on. How come?
2) The function returns a char(char rot13), but changed
is an integer. How is that possible?
推荐答案
cat /home/keisar/bin/c/ymse/rot13.h
char rot13(char character)
{
int change;
changed = character - ''a'' +''n'';
返回已更改;
}
我发现此代码有两个奇怪之处:<br />
1)我不必指定b应该被替换为n,
c by o等等。怎么样?
2)该函数返回一个char(char rot13),但是改变了
是一个整数。怎么可能?
cat /home/keisar/bin/c/ymse/rot13.h
char rot13(char character)
{
int changed;
changed = character - ''a'' + ''n'';
return changed;
}
I find two things strange about this code:
1) I don''t have to specify that b should be replaced by n,
c by o and so on. How come?
2) The function returns a char(char rot13), but changed
is an integer. How is that possible?
Eirik< hx ********************** ********@xyxaxhxoxo.no>潦草地写着以下内容:
Eirik <hx******************************@xyxaxhxoxo.no> scribbled the following:
这是我写的一个小功能,灵感来自线程
紧急帮助! Caesar Cipher所需要的
This is a little function I wrote, inspired by the thread
"Urgent HELP! required for Caesar Cipher PLEASE"
cat /home/keisar/bin/c/ymse/rot13.h
char rot13(char character)
{
int changed;
changed = character - ''a''+''n'';
返回已更改;
}
我发现此代码有两个奇怪之处:
1)我不必指定b应该用n代替,
c用o代替等等。怎么会?
实际上,如果我们对C标准严格要求,你必须指定b应该用n代替b等等。你碰巧是使用一个字符集来获得
,其中'''''''''''''是连续的,但C
标准允许使用其他字符集。
如果''''''''''''是连续的,上面的代码是有效的,因为
字符只是C中的整数类型。 />
2)该函数返回一个char(char rot13),但是更改
是一个整数。怎么可能?
cat /home/keisar/bin/c/ymse/rot13.h char rot13(char character)
{
int changed;
changed = character - ''a'' + ''n'';
return changed;
} I find two things strange about this code:
1) I don''t have to specify that b should be replaced by n,
c by o and so on. How come?
Actually, if we''re strict about the C standard, you DO have to
specify that b should be replaced by n and so on. You happen to be
using a character set where ''a''...''z'' are contiguous, but the C
standard allows for other character sets.
Provided ''a''...''z'' are contiguous, the above code works, because
chars are just integer types in C.
2) The function returns a char(char rot13), but changed
is an integer. How is that possible?
因为字符是整数类型。与Pascal不同,C不需要
字符之间的转换函数及其数值,
但是将它们视为可互换的。
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰----- --- \
\ - http:// www.helsinki.fi/~palaste ---------------------规则! -------- /
给我看一个好的mouser,我会告诉你一只口臭的猫。
- 加菲猫
Because chars are integer types. Unlike Pascal, C does not require
conversion functions between characters and their numeric values,
but treats them as interchangable by themselves.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Show me a good mouser and I''ll show you a cat with bad breath."
- Garfield
这篇关于为什么这样做? (rot13功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!