C 关于Rot13的实现
我正在尝试用C语言实现rot13算法。 但由于我对这种语言不是很熟悉,所以我的代码在这里遇到了一些问题 基本上,我想把args[]中的每个字母向上旋转13个位置。 但这段代码似乎相当缓慢:C 关于Rot13的实现,c,rot13,C,Rot13,我正在尝试用C语言实现rot13算法。 但由于我对这种语言不是很熟悉,所以我的代码在这里遇到了一些问题 基本上,我想把args[]中的每个字母向上旋转13个位置。 但这段代码似乎相当缓慢: #include <stdio.h> char[] rotate(char c[]) { char single; int i; int alen = sizeof(c)/sizeof(c[0]); char out[alen]; for(i=0;i<=alen;i+
#include <stdio.h>
char[] rotate(char c[]) {
char single;
int i;
int alen = sizeof(c)/sizeof(c[0]);
char out[alen];
for(i=0;i<=alen;i+=1) {
if(c[i]>='a' && (c[i]+13)<='z'){
out[i] = c[i]+13;
}
}
return out;
}
int main(int argc, char *argv[]) {
printf("The given args will be rotated\n");
int i;
char rotated[sizeof(argv)/sizeof(argv[0])];
rotated = rotate(argv);
/* printing rotated[] later on */
return 0;
}
#包括
字符[]旋转(字符c[]){
单焦;
int i;
int alen=sizeof(c)/sizeof(c[0]);
字符输出[alen];
对于(i=0;i='a'&&(c[i]+13)必须在编译时设置c中数组的大小,因此不能对数组大小使用非常量表达式
考虑以下实施:
// in place rotate
void rotate(char *str)
// str must be a zero-terminated string
{
int i =0;
// loop until str itself is not NULL and str[i] is not zero
for(i=0;str && str[i]; ++i) // ++i is a pre-increment
{
if(str[i] >= 'a' && (str[i]+13) <='z')
{
str[i] = str[i]+13; // modifying str in place
}
}
}
更新更高级的转换版本,在str[i]+13>“z”时处理大小写。
for(i=0;str && str[i]; ++i) // ++i is a pre-increment
{
// ignore out of range chars
if (str[i] < 'a' || str[i] > 'z') continue;
// rotate
for (off = 13; off > ('z' - str[i]); )
{
off-= (1 + 'z' - str[i]);
str[i] = 'a';
}
str[i]+=off;
}
for(i=0;str&&str[i];++i)//++i是一个预增量
{
//忽略超出范围的字符
如果(str[i]<'a'| | str[i]>'z')继续;
//轮换
对于(off=13;off>('z'-str[i]);)
{
off-=(1+'z'-str[i]);
str[i]=“a”;
}
str[i]+=off;
}
编译器不接受@Michael是如何说这个char out[alen]
的,因为您不能用非常量值声明数组大小。代码的另一个问题是for循环(i=0;i<=alen;i+=1)
阵列从0开始,因此如果您在长度的位置执行for操作,您将退出阵列
关于代码:
必须使用字符串开头的指针作为函数的参数,因为不能在C中返回数组(但可以返回指针)
你的if(str[i]>='a'&&(str[i]+13)='a'&&*(str+i)<'n')
*(str+i)+=13;
/*如果字母在n和z之间,你必须做相反的事情*/
否则如果(*(str+i)>='n'&&&*(str+i)谢谢大家,我用这段代码解决了这个问题
#include <stdio.h>
int rot13(int c){
if('a' <= c && c <= 'z'){
return rot13b(c,'a');
} else if ('A' <= c && c <= 'Z') {
return rot13b(c, 'A');
} else {
return c;
}
}
int rot13b(int c, int basis){
c = (((c-basis)+13)%26)+basis;
return c;
}
int main() {
printf("The given args will be rotated");
int c;
while((c = getchar()) != EOF){
c = rot13(c);
putchar(c);
}
return 0;
}
#包括
内部rot13(内部c){
如果('a'此函数可以对rot13字符串进行编码/解码。它与VIM的g?
rot13编码器兼容
void rot13 (char *s) {
if (s == NULL)
return;
int i;
for (i = 0; s[i]; i++) {
if (s[i] >= 'a' && s[i] <= 'm') { s[i] += 13; continue; }
if (s[i] >= 'A' && s[i] <= 'M') { s[i] += 13; continue; }
if (s[i] >= 'n' && s[i] <= 'z') { s[i] -= 13; continue; }
if (s[i] >= 'N' && s[i] <= 'Z') { s[i] -= 13; continue; }
}
}
void rot13(字符*s){
如果(s==NULL)
返回;
int i;
对于(i=0;s[i];i++){
如果(s[i]>='a'&&s[i]='a'&&s[i]='n'&&s[i]='n'&&s[i]您可能想学习使用调试器(如gdb
,如果您在Linux上)逐步完成代码,这将有助于获得启发。首先尝试使用gcc-Wall-Werror-pedantic-std=c99
(在Linux上):)我想说它会很慢,因为它永远不会完成(甚至开始)。char[]rotate(char c[]){/*…*/}
不会编译,因为函数不能返回数组。另外,你也不能将数组作为一个整体赋值(比如rotated=rotate(argv);
)。也许你可以更具体地说明你的问题.sizeof
不提供数组中的元素数。它提供大小(以字节为单位)对象类型。当您接收数组作为参数时,实际上只是接收一个指向数组的指针。数组的逻辑大小可以是任意大小;无法判断。毕竟,它只是内存中的字节。因此,sizeof
可以告诉您的是指针的大小。此外,执行旋转是非旋转的。考虑C[i]是“n”或“更大”时会发生什么。你可能还想看看ARCC和ARGV是如何工作的。旋转的意思是什么?正如Antoine Mathys指出的那样,在我的回答中指出,变换是不正确的,但是对于<代码> + 1=0;STR & STR[i]。;++i
为正确的ROT13添加了解决方案,该解决方案也适用于ROT(N)@MichaelSh您声称数组的大小必须是编译时常量的说法仅适用于C99之前的C,C99引入了可变长度数组。尽管最近在当前的C11标准中,这被降级为可选功能。
#include <stdio.h>
int rot13(int c){
if('a' <= c && c <= 'z'){
return rot13b(c,'a');
} else if ('A' <= c && c <= 'Z') {
return rot13b(c, 'A');
} else {
return c;
}
}
int rot13b(int c, int basis){
c = (((c-basis)+13)%26)+basis;
return c;
}
int main() {
printf("The given args will be rotated");
int c;
while((c = getchar()) != EOF){
c = rot13(c);
putchar(c);
}
return 0;
}
void rot13 (char *s) {
if (s == NULL)
return;
int i;
for (i = 0; s[i]; i++) {
if (s[i] >= 'a' && s[i] <= 'm') { s[i] += 13; continue; }
if (s[i] >= 'A' && s[i] <= 'M') { s[i] += 13; continue; }
if (s[i] >= 'n' && s[i] <= 'z') { s[i] -= 13; continue; }
if (s[i] >= 'N' && s[i] <= 'Z') { s[i] -= 13; continue; }
}
}