4

The problem comes because cipher is declared a char array here char cipher[strlen(plain)];, but printed out using a string (aka char*) format here printf("ciphertext: %s\n", cipher);. Remember, the thing that makes a string a string is the terminating null byte \0. And a string occupies one more byte than strlen to accomodate the terminator. cipher is not ...


2

You have nested loops. Don't. While you do have two indices (one in the text and one in the key), the index for the key is not independent. It starts at 0 as well and is incremented at encrypting a letter (but would stay the same at passing a non-letter, so you could add spaces and punctuation to the text and it wouldn't affect the letters after encryption). ...


2

There are a number of problems with this code. Let's deal with your question first. The code is constructed as a nested pair of loops. The outer for loop will loop over the length of the plain text to be encoded. The inner for loop will loop over the key. That means that for EACH character in the plain text, the entire key will be looped over. Why? The ...


1

Your issue is a misplaced ) when you calculate the key. int letterKey = tolower(k[j % kLen] - 'a'); You want to lowercase the key and then subtract 'a'. Move your closing brace.


1

Re: the for loop. A for loop structure has 3 sections: initialization, test, increment. Each section is separated by semicolons. Within each section, there can be multiple parts. Those parts are separated by commas. In this case, there are two initializations, for i and n. They are separated by the comma. "Control reaches end of non-void function" means ...


1

I would recommend you to do this problem set in one for loop. You can just loop through the plaintext. Using nested loops will make the problem more complicates than you think. It's because you only need to loop through the plaintext and do changes to each character in the plaintext. Some other advice with the code is, what if the plaintext is upper case, ...


1

Very common error. The key index, k, is being incremented with EVERY plaintext char processed (incremented in the for loop setup). It should ONLY be incremented when an alpha is processed. Keep it simple! Don't overthink it! Here's a hint.... { printf( <an encoded letter> ); k++; // or k = (k+1) % something; } If this answers your question,...


1

You're right. The compiler just knows that there's a possibility of getting to the end of the code without seeing a return statement, so it's unhappy. However, your fix in the comments has a flaw. What happens when a non-alpha is passed to this function? It will return a bad value because it will be treated as a lower case letter. Instead, you could have ...


1

There are a number of issues in the code that are impacting it. First, look at this: int *keyInt = malloc(strlen(key)); This allocates n bytes where n is the number of chars in the key string. However, the pointer keyInt is a pointer to a single integer. Even with that, a char is one byte, an int is 4 bytes, so if you're converting a char to an int, ...


1

Since you declared int index = 0; inside the loop, it gets reset to 0 for each character, you always stay on the b of bacon. The fix would be to move that above your loop, outside the loop body.


1

* after a type is used to make a pointer. A pointer stores a memory address, and the pointer having that particular type will tell the compiler how to interpret the memory content when you dereference the pointer (you can cast pointers to different types, interpreting same memory differently, like Quake interpreting 4 bytes sometimes as float, sometimes as ...


1

When checking for non-alphabetic characters, your loop should do exactly that, nothing more. After that loop, when you know no non-alphabetic character was in the keyword (as you would have returned otherwise), ask for plain text. So it's basically moving one } from the end up to after what you showed initially.


1

atoi takes a string as argument. You are passing it a single char. As an example, if you have atoi("123"); you'll get 123. But you can't pass it atoi('1') as that is a char. If you are trying to get the ascii value of each char in the keyword, you can subtract either 'A' or 'a' from the char, such that if argv[1][a] was 'c', then 'c' - 'a' is 2. I ...


1

The good news is that you got the concept right. J needs to be reset at a certain point. The problem with that lies here: if (j >= strlen(k) - 1) Why - 1??? There is an additional problem that maybe you haven't identified yet. What happens when the plaintext and the key are different cases? And now, some notes on efficient programming. The first ...


1

I really don't know where your numbers start and end, and what they refer to (I don't think that's the output of the code you show), at least some whitespace between would have been nice. Also, the non-alpha part seems to work fine. You need to apply some wrap-around. If shifting a 'Z' further forward, it should become 'A' again. You can achieve this by ...


1

The integer to pointer conversion does not happen at s[i] - 65 (which produces an int), but at return. You stated that your function returns a string, which is an alias for char*, a pointer to char. Now you try to return an integer instead. Maybe you meant to do something like printf with that number instead of returning?


Only top voted, non community-wiki answers of a minimum length are eligible