#I need your help to finish my encrypting algorithm

8 messages · Page 1 of 1 (latest)

runic orbit
#

So i want to learn new things. My idea for text encryption is to increase the ascii value of a char in a char pointer of its index like char[1] will increase the ascii number by 2 because its the 2nd element. If the ascii number is bigger than 90 (Z) it starts from 65 (A) and continues increasing (its for capital letters only). When the index is bigger than 26 it will probably decrease number i increase the chars but it doesnt matter. I have to fix this code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int *encrypt(char *src, char *dst) {
    for(int i=0; i<=strlen(src); i++) {
        for(int j=i+1; j>0; j--) {
            if(src[i] < 65 || src[i] > 90) {
                dst[i] = ' ';
                break;
            } else
                dst[i] = src[i];
            if(dst[i] == 90)
                dst[i] = 65;
            else
                dst[i] ++;
        }
    }

    return 0;
}

int main(int argc, char *argv[]) {
    char *src, *dst = malloc(256);
    fgets(src, 255, stdin);
    encrypt(src, dst);
    printf("%s", dst);

    return 0;
}```
My problem is that in the encrypt function all characters increase by 1 only. Im not sure why, the loop executes multiple times however every letter become increased by one after the function finishes which isnt what i expect.
shut martenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

royal quail
#

First of all: i shouldn't run until <= strlen but rather until < strlen. Not to mention that putting strlen in the loop condition like that is horrible because it needs to be evaluated every iteration.
Second it's really not a smart idea to increment the number one at a time. Why not do all n incrementation at once?
Also doing char *src, *dst = malloc(256); only initializes dst. src isn't initialized yet.
Also why does your encrypt function return int *, but then you just do return 0 AND THEN you don't even use that return value.
Furthermore I think (not sure on this) it's convention that you put dest as the first parameter and src as the second.
Not to mention that instead of using 65 and 90 you should just use 'A' and 'Z'.
Let's fix this code and make it much more efficient at the same time:

#

;compile | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void encrypt(char *dest, char *src) {
    const int mod = 'Z' - 'A' + 1;
    int n = strlen(src);
    memcpy(dest, src, n + 1);
    for (int i = 0; i < n; ++i) {
        if (dest[i] < 'A' || dest[i] > 'Z') {
            dest[i] = ' ';
            continue;
        }
            
        dest[i] = ((dest[i] - 'A' + i + 1) % mod) + 'A';
    }
}

int main() {
    char src[100], dest[100];
    fgets(src, 100, stdin);
    encrypt(dest, src);
    printf("src: %s\ndest: %s\n", src, dest);
}
plucky dirgeBOT
#
Program Output
src: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
dest: BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI
runic orbit
#

wow