4

Think about what this code is doing; int main(int argc, string argv[]) { int n = strlen(argv[1]); If there's no argument, then argv[1] doesn't exist. Trying to access it generates a seg fault. That's why it's necessary to check the value of argc before trying to use any argv element. If this answers your question, please click on the check mark to ...


3

When encrypt[n] is declared, it doesn't allow for the end of string marker at the end. Also, the end of string marker is never added to the string, so when it is printed at the end of the program, garbage data following the string is also printed, until it finds random data that looks like the end of string marker. That answers your question, but there are ...


2

You want hints? Except for the first initial, what character is always before every initial? There are a lot of library functions out there, some that you should look at now, like tolower(), isalpha(), and a whole lot more. Since you only wanted a hint, I'll let you think about what else is out there. ;-) If this answers your question, please click on ...


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

First of all, the code MUST check for lack of a key, i.e., a valid number of parameters by checking the value of argc FIRST!!! Right now, the code is trying to use argv[1] before checking the value of argc, meaning that it's trying to use an array element that might not even exist! That should solve some of your issues. Next, there's no code at all to ...


1

I'd test exactly for a space (ie text[i] == ' ') rather than isblank and adding 1 at the end is expected, since if you have text like "Hello there", you'll count 1 space but there are 2 words. Just be sure you update your words variable so your later calculation will be correct. (alternatively, initialize words to 1, since you know you won't ever have zero)...


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

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

Use <, not <=, in your loop. For i being strlen(argv[1]), argv[1][i] is '\0', which is not alphabetic, so you leave the function (and end the programme).


1

First to the error: i<len,k>=0 uses the comma operator, which evaluates its left side, discards the result, and evaluates to its right side. So i<len,k>=0 is the same as k>=0, the value of i<len is ignored. You're doing Caesar's cipher, right? This one looks like a strange crossover of caesar and vigenère. In Caesar's cipher, you shift ...


1

According to the spec, the key should only be advanced when it is used. Considering that, when you print a special character (non-alphabetical), as well as making sure that it is printed unchanged, the key should not be advanced. This is how it should be: INPUT: world, say hello! KEY: bazba zba zbazb OUTPUT: xoqmd, rby gflkp! And this is what your ...


1

Without seeing the actual error message or the related code, this is a problem: printf("%c", toupper(s[(int)(' ')]i++)); There is one placeholder in the format string, the %s, but the value to be substituted is ambiguous at best. The combination of toupper(s[(int)(' ')] and i++ is not a valid parameter here. It is two parameters where there should only be ...


1

Simply put, your formula is flawed. Look at the formula that you have: code = code + user_key % 26; Assuming that you haven't altered the formula in your conversion to pseudocode, it has two problems. First, modulo has the same precedence rules as multiplication or division, so you would only be applying it to user_key and not to code or the sum of the ...


1

I see you're incrementing j at the end of every iteration of the for loop then decrementing it in case the current char is not alphabetical. well, you could increment it only when you need to, right? are you sure you wanna check whether key[p] is less than 26? also do you really have to do that every time either of the first two condition is true?


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