BillyYu

CAESAR加密 操练

先贴Caesar Cipher的wikipedia.
#include <stdio.h>
#include 
<cs50.h>
#include 
<ctype.h>
#include 
<stdlib.h>
#include 
<string.h>

int
main(
int argc, char *argv[])    {
    
//check the argument
    if (argc != 2) {
        printf(
"You should input ONE,and ONLY ONE argument.\n");
        
return 1;
    }

    
int KEY = atoi(argv[1]);
    
    printf(
"Plain Text:");
    
char *text=GetString();

    
char *tmp=(char *)malloc(sizeof(char* strlen(text));
    memcpy(tmp,text,strlen(text));

    
int i=0;
    
while(tmp[i] != '\0')    {
        
if ( isalpha(tmp[i]))    {
            
if ( isupper(tmp[i]) ) {
                tmp[i]
=(tmp[i] - 'A' + KEY)%26 + 'A';
            } 
else    {
              tmp[i]
=(tmp[i] - 'a' + KEY)%26 + 'a';
            }
        }
        i
++;
    }

    printf(
"Cypher: %s\n",tmp);
    free(tmp);
    
return 0;
}

posted on 2011-01-17 23:23 志华 阅读(400) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜