Data structure Practice--string encryption

Source: Internet
Author: User

This article is aimed at the data structure Basic Series Network course (4): String Practice Project.

"Project-String Encryption"
A text string can be encrypted using a pre-compiled character mapping table. For example, set the character mapping table to:

abcdefghijklmnopqrstuvwxyzngzqtcobmuhelkpdawxfyivrsj

The string "Lao he jiao Shu Ju Jie Gou" is encrypted as "ENP BT UMNP Xby uy umt opy".
Design a program to implement encryption, decryption algorithm, the input text is encrypted after the output, and then decrypted and output.

[Refer to answer] (header file sqstring.h see sequential string algorithm library)

#include <stdio.h>#include "sqString.h"Sqstring A, B;//For storing character mapping tablesSqstring EnCrypt (sqstring p) {int i=0, J; Sqstring q; while(I<p.length)    { for(j=0; P.DATA[I]!=A.DATA[J]; j + +);if(J>=p.length)//P.data[i not found in string a)Q.data[i]=p.data[i];Else                        //Find the p.data[i] letter in a stringQ.DATA[I]=B.DATA[J];    i++; } q.length=p.length;returnQ;} Sqstring unencrypt (sqstring q) {int i=0, J; Sqstring p; while(I<q.length)    { for(j=0; Q.DATA[I]!=B.DATA[J]; j + +);if(J>=q.length)//Q.data[i not found in string b)P.data[i]=q.data[i];Else                    //Find the Q.data[i] letter in string bP.DATA[I]=A.DATA[J];    i++; } p.length=q.length;returnP;}    int main () {sqstring p,q; Strassign (A,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");//Create a stringStrassign (B,"NGZQTCOBMUHELKPDAWXFYIVRSJ");//Build B stringChar Str[maxsize];printf("\ n");printf("input source string:"); Gets (str);//Get the source string of the user inputStrassign (P,STR);//Build P string    printf("Encryption decrypted as follows: \ n");printf("source string:");    DISPSTR (P); Q=encrypt (P);//p string encryption generates Q string    printf("Encrypt string:");    DISPSTR (q); P=unencrypt (q);//q string decryption generates P-string    printf("decryption string:"); DISPSTR (P);printf("\ n");return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure Practice--string encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.