Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

encryption

rwia123rwia123 Member Posts: 15
I have 2 arrays. One is {'a', 'b, 'c', etc.} the second is {'z', 'y', 'x', etc.}. My objective is to run a function to encrypt an input of a to z, c to x, ect. i.e. abc to zyx. If I use a "for" loop, how do I use without pointers. I'm getting the first letter but then a bunch of ascii after.

Thanks for you help.

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : I have 2 arrays. One is {'a', 'b, 'c', etc.} the second is {'z', 'y', 'x', etc.}. My objective is to run a function to encrypt an input of a to z, c to x, ect. i.e. abc to zyx. If I use a "for" loop, how do I use without pointers. I'm getting the first letter but then a bunch of ascii after.
    :
    : Thanks for you help.
    :
    [blue]If your input is in range ['a'..'z'] use (input_ascii-'a') as an index into the second array:[/blue][code]
    char* pstrEnCoder = "zyxwvutsrqponmlkjihgfedcba";
    char* pstrIn = "hello";
    char strOut [10];
    int iLen = strlen (pstrIn);

    for (int i=0; i<iLen; i++) {
    strOut [i] = pstrEnCoder [pstrIn[i]-'a'];
    }
    strOut [iLen] = '';
    [/code]
  • rwia123rwia123 Member Posts: 15
    : : I have 2 arrays. One is {'a', 'b, 'c', etc.} the second is {'z', 'y', 'x', etc.}. My objective is to run a function to encrypt an input of a to z, c to x, ect. i.e. abc to zyx. If I use a "for" loop, how do I use without pointers. I'm getting the first letter but then a bunch of ascii after.
    : :
    : : Thanks for you help.
    : :
    : [blue]If your input is in range ['a'..'z'] use (input_ascii-'a') as an index into the second array:[/blue][code]
    : char* pstrEnCoder = "zyxwvutsrqponmlkjihgfedcba";
    : char* pstrIn = "hello";
    : char strOut [10];
    : int iLen = strlen (pstrIn);
    :
    : for (int i=0; i<iLen; i++) {
    : strOut [i] = pstrEnCoder [pstrIn[i]-'a'];
    : }
    : strOut [iLen] = '';
    : [/code]
    :

  • rwia123rwia123 Member Posts: 15
    This is what I originally had - not sure how to add your insight. The
    information helped but getting other errors. Your help would be
    appreciated.


    int get_encryp()

    {

    char reg[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    char encryp[26] =
    {'Q', 'H', 'R', 'B', 'X', 'Y', 'C', 'I', 'G', 'P', 'S', 'A', 'J', 'V', 'W', 'K', 'Z', 'E', 'T', 'L', 'O', 'F', 'N', 'M', 'U', 'D'};

    int temp[26];
    int inp[25];
    int i = 0;

    printf("You answered E. for encryption.
    ");
    printf("Input text to be encrypted.
    ");
    inp[25] = getchar();

    for (i = 0; i < 25; i++)
    if (inp[i] == reg[i])
    {
    reg[i] = encryp[i];
    temp[i] = reg[i];
    }

    for (i = 0; i < 25; i++)
    putchar(temp[i]);

    return;
    }





    : : : I have 2 arrays. One is {'a', 'b, 'c', etc.} the second is {'z', 'y', 'x', etc.}. My objective is to run a function to encrypt an input of a to z, c to x, ect. i.e. abc to zyx. If I use a "for" loop, how do I use without pointers. I'm getting the first letter but then a bunch of ascii after.
    : : :
    : : : Thanks for you help.
    : : :
    : : [blue]If your input is in range ['a'..'z'] use (input_ascii-'a') as an index into the second array:[/blue][code]
    : : char* pstrEnCoder = "zyxwvutsrqponmlkjihgfedcba";
    : : char* pstrIn = "hello";
    : : char strOut [10];
    : : int iLen = strlen (pstrIn);
    : :
    : : for (int i=0; i<iLen; i++) {
    : : strOut [i] = pstrEnCoder [pstrIn[i]-'a'];
    : : }
    : : strOut [iLen] = '';
    : : [/code]
    : :
    :
    :

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [b][red]This message was edited by AsmGuru62 at 2002-10-28 8:28:21[/red][/b][hr]
    : This is what I originally had - not sure how to add your insight. The
    : information helped but getting other errors. Your help would be
    : appreciated.
    :
    :
    : int get_encryp()
    :
    : {
    :
    : char reg[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    :
    : char encryp[26] =
    : {'Q', 'H', 'R', 'B', 'X', 'Y', 'C', 'I', 'G', 'P', 'S', 'A', 'J', 'V', 'W', 'K', 'Z', 'E', 'T', 'L', 'O', 'F', 'N', 'M', 'U', 'D'};
    :
    : int temp[26];
    : int inp[25];
    : int i = 0;
    :
    : printf("You answered E. for encryption.
    ");
    : printf("Input text to be encrypted.
    ");
    : [red][b]inp[25] = getchar(); //max ndx is 24[/red][/b]
    [blue]// Besides, I do not think you will get the whole string like that
    // You need to use string input function...[/blue]
    :
    : for (i = 0; i < 25; i++)
    : if (inp[i] == reg[i])
    : {
    : reg[i] = encryp[i];
    : temp[i] = reg[i];
    : }
    :
    : for (i = 0; i < 25; i++)
    : putchar(temp[i]);
    [blue]// Some of the symbols in [b]temp[/b] will be printed as unitialized - i.e. garbage...[/blue]
    :
    : return;
    : }
    :
    :
    :
    :
    :
    : : : : I have 2 arrays. One is {'a', 'b, 'c', etc.} the second is {'z', 'y', 'x', etc.}. My objective is to run a function to encrypt an input of a to z, c to x, ect. i.e. abc to zyx. If I use a "for" loop, how do I use without pointers. I'm getting the first letter but then a bunch of ascii after.
    : : : :
    : : : : Thanks for you help.
    : : : :
    : : : [blue]If your input is in range ['a'..'z'] use (input_ascii-'a') as an index into the second array:[/blue][code]
    : : : char* pstrEnCoder = "zyxwvutsrqponmlkjihgfedcba";
    : : : char* pstrIn = "hello";
    : : : char strOut [10];
    : : : int iLen = strlen (pstrIn);
    : : :
    : : : for (int i=0; i<iLen; i++) {
    : : : strOut [i] = pstrEnCoder [pstrIn[i]-'a'];
    : : : }
    : : : strOut [iLen] = '';
    : : : [/code]
    : : :
    : :
    : :
    :
    :
    [blue]If I get it correct... you have two arrays: one with original letters and one with a replacement letters, so if you see the letter in the sentence - you replace it from the array which can be called an encryption key. If so, then try this:[/blue][code]
    char original [26] = { /* fill it up...*/ };
    char encryption_key [26] = { /* fill it up...*/ };
    char input_buf [64];
    char* input;
    int iLenInput, i, k, chAscii;
    int iKeyLen = sizeof (encryption_key);

    printf ("
    Enter the text for encryption:
    ");

    [b]// Get the input:[/b]
    input_buf [0] = 60; [b]// Max chars to enter[/b]
    input = cgets (input_buf); [b]// input points to the text entered[/b]

    [b]// Replacing:[/b]
    iLenInput = strlen (input);
    for (i=0; i<iLenInput; i++) {
    chAscii = input [i];

    [b]// Looking for a replacement:[/b]
    for (k=0; k<iKeyLen; k++) {
    if (original [k] == chAscii) {
    [b]// Found it![/b]
    input [i] = encryption_key [k];
    break; // No need to scan more
    }
    }
    }

    [b]// Print the encrypted input:[/b]
    printf ("

    Encrypted:
    %s
    ", input);
    [/code]


  • Shawn CarterShawn Carter Member Posts: 0

    ___ \ http://forcoder.org \ free video tutorials and ebooks about [ C Perl PL/SQL Go Delphi Scratch Assembly Python JavaScript C++ R MATLAB Swift Ruby Java Visual Basic PHP C# Visual Basic .NET Objective-C Dart Julia Apex Clojure COBOL Lisp Alice Scheme Ada Awk D Crystal VBScript Erlang Rust LabVIEW Lua Kotlin Bash Transact-SQL ABAP F# Fortran SAS Scala ML Prolog Logo Hack FoxPro ] _____

Sign In or Register to comment.