0

To solve caesar's cipher, I'm trying to concatenate each character onto the end of the cipher string (as c[i] = "blah" didn't work). strcat isn't working for me, though. First of all, this thing below produces a segmentation fault (whatever that is...).

int main(void)
{
    string s = "a";
    string new = strcat(s, "a");
    printf("%s\n", new);
}

Or maybe someone can give a quick suggestion on a better way to build the cipher string. Idk, I could have knocked this problem out in 5 minutes with Ruby or Python, but C syntax is kicking my...

0

1 Answer 1

2

First, strcat doesn't work because of the fact that the string literal "a" that's stored in s is stored into a read-only-data section in memory. So you cannot modify its contents.

A simple fix to that is to use a char array that's big enough to fit the concatenated strings as well as the null terminator. So

char dest[6];
strcat(dest, "hello");

should work just fine!

A better suggestion would be to print the encrypted char directly to the terminal since you don't really wanna store it.

1
  • "A better suggestion would be to print the encrypted char directly to the terminal since you don't really wanna store it." This is what I really needed!!
    – SeanIvins
    Nov 4, 2014 at 20:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .