0

The program must encrypt the characters in a string adding 2 to those located in even positions and 3 to those in odd positions. I declare string as a pointer because I do not know the length of the string that the user will enter. The program compiles, but stumbles.

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

/*PROGRAM EXERC107*/
int main() {
    char * string;
    int encryp, i;
    string = 0;
    printf("Enter a string for encrypting:");
    scanf("%s",string);
    printf("\n");

    for (i=0; i < strlen(string);i++)
    {
        if ((i % 2)==0)
        {
            encryp=string[i];
            string[i]=encryp + 2;
        }
        else
        {
            encryp=string[i];
            string[i]=encryp + 3;
        }

        printf("%c",string[i]);
    }
    getch();
    return 0;
}
Alejandro Caro
  • 998
  • 1
  • 10
  • 22
  • Dereferencing `NULL` will invoke *undefined behavior*. Allocate some buffer before trying to read something in it. By the way, what is your question? – MikeCAT Jul 09 '16 at 15:29
  • I'm asking because the program stumbles. – Alejandro Caro Jul 09 '16 at 15:39
  • it's because you are not assigning memory to the string... this might help you... it doesn't run on the website but it runs on my laptop : https://ideone.com/3pd4nB @AlejandroCaro – Cherubim Jul 09 '16 at 15:52
  • @AlejandroCaro i forgot to add `free(string);` before `return 0;` in the link... dont forget to free the aloocated memory – Cherubim Jul 09 '16 at 16:40

0 Answers0