字符后移的通用办法

Q: n = 4, w,x,y,z超出字符集

初始粗糙透顶的方法

#include <stdio.h>

//定义main函数

int main()

{

char c1,c2,c3,c4,c5;

//请在此添加‘字符加密’的代码

/*****************Begin******************/

scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&c5);

printf("%c%c%c%c%c",c1+4,c2+4,c3+4,c4+4,c5+4);

/***************** End ******************/

return 0;

}

#include <stdio.h>
#include <string.h> 
char caesar_cipher(char *text,int shift)
{
	int i=0;
	char ch;
	for(i=0;text[i] != '\0';i++)
	{
		ch = text[i];
		if(ch>='A'&&ch<='Z') 
		{
		   ch = 'A' + (ch-'A'+shift)%26;
		}
		if(ch>='a'&&ch<='z')
		{
			ch = 'a'+(ch-'a'+shift)%26;
		}
	    text[i]=ch;
	}
	
}
int main()
{
	char text[]="Hello World";
	int shift = 5;
	
	caesar_cipher(text,shift);   //array point定义和使用的方法 
	printf("Encrypted:%s",text);
	return 0;
 } 

看看学校的垃圾答案

    #include <stdio.h>
    //定义main函数
    int main()
    {
        char c1,c2,c3,c4,c5;
        //请在此添加‘字符加密’的代码
        /*****************Begin******************/
        c1=getchar();
        c2=getchar();
        c3=getchar();
        c4=getchar();
        c5=getchar();
        c1 +=4;
        c2 +=4;
        c3 +=4;
        c4 +=4;
        c5 +=4;
        c1 =c1 > 'z' ? c1-26 : c1; 
        c2 =c2 > 'z' ? c2-26 : c2;
        c3 =c3 > 'z' ? c3-26 : c3;
        c4 =c4 > 'z' ? c4-26 : c4;
        c5 =c5 > 'z' ? c5-26 : c5;
        printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);
        /***************** End ******************/
        return 0;
    }


包括 基础语法 条件 循环 函数

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务