C language | Translate the password back to the original text, and output the password and original text

Example 66: There is a line of text, which has been translated into a password according to the following rules: A->Z a->z; B->Y b->y; that is, the first letter becomes the 26th letter, and the i-th letter becomes Into the (26-i+1)th letter, non-letter characters remain unchanged. C language programming is required to translate the password back to the original text, and output the password and original text.

Analysis: You can define an array ch to store messages in it. If the character ch[j] is an uppercase letter, it is the (ch[j]-64)th uppercase letter among the 26 letters. Because the password is symmetrically converted, the first letter is converted to the last letter , The last one is converted to the first letter, so the translation from the original to the password and the translation from the password to the original are the same formula.

Source code demo:

#include<stdio.h>//头文件 
int main()//主函数 
{
    
    
  int j,n;//定义整型变量 
  char ch[80],tran[80];//定义字符数组 
  printf("输入密码:\n");//提示语句 
  gets(ch);//键盘输入 
  printf("\n密码是:\n%s",ch);//密码 
  j=0;//赋初值 
  while(ch[j]!='\0')//不是最后一个字符时 
  {
    
     
    if((ch[j]>='A')&&(ch[j]<='Z'))//ASCII中A对应的值是65,a对应的值是97 
    {
    
    
      tran[j]=155-ch[j];
    }
    else if((ch[j]>='a')&&(ch[j]<='z'))//小写 
    {
    
    
      tran[j]=219-ch[j];
    }
    else
    {
    
    
      tran[j]=ch[j];
    }
    j++;
  }
  n=j;
  printf("\n输出原文:\n");//提示语句 
  for(j=0;j<n;j++)//遍历输出 
  {
    
     
    putchar(tran[j]);
  } 
  printf("\n");//换行 
  return 0;//函数返回值为0 
}

The compilation and running results are as follows:

输入密码:
C yuyan

密码是:
C yuyan
输出原文:
X bfbzm

--------------------------------
Process exited after 9.542 seconds with return value 0
请按任意键继续. . .

Above, if you see it and think it is helpful to you, please give Xiaolin a thumbs up and share it with the people around him, so that Xiaolin will also have the motivation to update, thank you fathers and villagers~

C language translates the password back to the original text, and outputs the password and original text.
More cases can go to the public account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112959850