首页 > 软件 > 请编程序将输入的字符串译成密码,

请编程序将输入的字符串译成密码,

软件 2026-07-12

c语言编写:将用户输入的字符串翻译成密码串:规则如下?

a b c d e f g h i j 例子里输入ab,cd a是第1个字符,那么换成a后面第1个,也就是b b是第2个字符,那么换成b后面第2个,也就是d 到这里都没错 c是第4个字符,那么换成后面第4个,也就是g,为什么题目示例是h??? #include #include int main() { int i=0; char str[1001]; fgets(str,1000,stdin); while(str[i]){ if(islower(str[i])) str[i]=97+(str[i]+i-96)%26; else if(isupper(

编写程序,将输入的字符串译成密码,译码规律是:用原来的字母后面的第4个字母代替原来的字母.

#include
#defineN256
voidmain(){charstr[N];inti;
scanf("%s",str);
//i=0;
//while(1){
//str[i]=getchar();if(str[i]==10)break;
//i++;if(i>=N-1)break;
//}
//str[i]=0;
i=0;
while(str[i]!=0){
if(str[i]>='a'&&str[i]<='z')str[i]=(str[i]-'a'+4)%26+'a';
elseif(str[i]>='A'&&str[i]<='Z')str[i]=(str[i]-'A'+4)%26+'A';
i++;
}
printf("%s\n",str);
}

C++:要将任意给定字母组成的字符串译成密码。

#include
usingnamespacestd;
#defineSKIP4
intmain(){
charstr[21]="China#";
cout<<"译码前:"<<"\b"<//这里跳过合法性检查。
for(inti=0;i<21&&str[i]!='#';i++){
if(str[i]>=65&&str[i]<=90){
if(str[i]+SKIP>90)str[i]=65+(str[i]+SKIP-90);//重新到回头部。
elsestr[i]=str[i]+SKIP;
}
else{
if(str[i]+SKIP>122)str[i]=97+(str[i]+SKIP-122);//重新到回头部。
elsestr[i]=str[i]+SKIP;
}
}
cout<<"译码后:"<<"\b"<return0;
}

另外多说一句,如果A译成E,那么你举的例子:“Who”应译为“Als”就不正确了,应该是“Who”应译为“Bls”才正确。

一道C++问题翻译密码

#include using namespace std; int main() { char c1 = 'C', c2 = 'h', c3 = 'i', c4 = 'n', c5 = 'a'; c1 += 4; c2 += 4; c3 += 4; c4 += 4; c5 += 4; cout << c1 << c2 << c3 << c4 << c5; }

请编程序将:输入单词译成密码,密码规律是:用原来的字母后面的第4个字母代替原来的字母。

C语言程序:

#include
#include
#defineMAX100
intisValidate(charstr[]);
intisLetter(charch);
intisLow(charch);
voidencrypt(charsource[],chardest[]);
voidmain()
{
charsource[MAX];
chardest[MAX];
printf("inputastring:");
gets(source);
if(isValidate(source)==0)
{
printf("error\n");
return;
}
if(strlen(source)>20)
{
source[20]='\0';
}
encrypt(source,dest);
printf("encrypted:%s\n",dest);
}
/*判断字符串str是否合法*/
intisValidate(charstr[])
{
inti,len;
len=strlen(str);
if(len<=0)
{
return0;
}
for(i=0;i {
if(isLetter(str[i])==0)
{
return0;
}
}
return1;
}
/*判断字符ch是否是字母*/
intisLetter(charch)
{
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
return1;
}
else
{
return0;
}
}
/*判断字符ch是否是小写字母*/
intisLow(charch)
{
if(ch>='a'&&ch<='z')
{
return1;
}
else
{
return0;
}
}
/*加密字符串*/
voidencrypt(charsource[],chardest[])
{
intlen=strlen(source);
for(inti=0;i {
if(isLow(source[i])==1)
{
dest[i]=(source[i]-'a'+4)%26+'a';
}
else
{
dest[i]=(source[i]-'A'+4)%26+'A';
}
}
dest[i]='\0';
}


运行测试:

inputastring:China
encrypted:Glmre

标签:编程语言 密码 字母 CC++ 编程

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved. 京ICP备11019930号-18