0

So I’ve figured out 99% of the code for the substitution problem. My plaintext input is being output correctly, with the correct enciphered letters and the correct case.

However, I keep getting extra chars after the output. See photo for an example.

Any ideas as to why this is happening and how to fix it, aka to only output the enciphered version of the plaintext input?

Does this have something to do with the NUL character stored at the end of the input string? If so, how would I avoid outputting that?

Thanks! enter image description here

UPDATE: I am trying to figure out how / where to insert the \0 in order to end the array after the encoded letters have been stored. I'll attach photos below. How should I change this, so that I don't keep getting segmentation fault errors but also don't keep outputting too many chars? enter image description here enter image description here

1 Answer 1

0

"Does this have something to do with the NUL character stored at the end of the input string? If so, how would I avoid outputting that?"

Close. Without seeing your code, I'll bet that it places the encoded letters into another string array, but not the end of string marker, the null character at the end.

Read this recent question on the same issue: why I have additonal output for caesar program?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • I will try that next. That makes sense Nov 11, 2022 at 21:47
  • So i attempted to set the size of the array (char cipher[N]) into which I'm sorting the enciphered chars as the length of the plaintext input by the user, plus 1. (int N = lenplain + 1;). Then I tried to insert \0 outside the for loop that was populating cipher[]. I'll post the code above. But i am either getting segmentation fault errors, or, if i run the program with a key included, i get the same problem as before with an extra char in the output Nov 14, 2022 at 12:44
  • Ok i think i figured it out. I was trying to assign the null char with cipher[N] = '\0';, where N was k (the length of the user input plaintext), plus 1. That didn't work. But when i use cipher[k] = '\0';, the code works without printing extra chars. Since I set N = k+1 earlier in the code, I still don't understand why the first format didn't work while the second format did?? Nov 14, 2022 at 15:12
  • The first method was overwriting the first byte of whatever was next in memory and allowing one extra stray character to be written out. Perhaps you forgot that the first element of an array is indexed as 0 and not 1, as in cipher[0] and not cipher[1]. Remember, if N is the length of an array, the last element is N-1. So, say you have the word "cat" having length 3. Then, add 1 for the null element and the array has to have length 4. BUT, the elements will be numbered 0,1,2 and 3. The null value goes into 3. Bottom line, put the null into cipher[ sizeof(word) ]
    – Cliff B
    Nov 14, 2022 at 23:49

You must log in to answer this question.

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