标准输入读取字符简单加密标准输出

//

//  main.c

//  testC

//

//  Created by KONY on 13-6-16.

//  Copyright (c) 2013 KONY. All rights reserved.

//  编写一个程序,从标准输入读取字符,并把他们写到标准输出中。所有非字母字符都完全按照它的输入形式输出,字母字符在输出前进行加密。加密方法很简单:每个字母被修改为字母表上距其13个位置(前或后)的字母。例如,A被修改为NB被修改为OZ被修改为M,以此类推。注意大小写字母都应该被转换。

//

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

void encryptStr(char *tempStr);

int changeWorld(int ch,int base);

 

int main(int argc, const char * argv[])

{

    char *inputStr = malloc(1024*10);

    while (fgets(inputStr, 100, stdin) != NULL)

    {

        printf("input string :%s\n",inputStr);

        encryptStr(inputStr);

        printf("parse the string: %s\n",inputStr);

        printf("=============================\n");

    }

    return 0;

}

 

 

void encryptStr(char *str)

{

    char *temp = NULL;

    for(temp = str;*temp;temp++)

    {

        if(*temp >= 'A' && *temp <= 'Z')

            *temp = changeWorld(*temp, 'A');

        if(*temp >= 'a' && *temp <= 'z')

            *temp = changeWorld(*temp, 'a');

    }

}

 

int changeWorld(int ch,int base)

{

    ch -= base;

    ch += 13;

    ch %= 26;

    return ch + base;

}

 

 

 

posted @ 2013-06-20 20:15  free0103  阅读(149)  评论(0)    收藏  举报