0

Scroll down for edited question about Check50 hello world!

The Segmentation fault and encipher plaintext question has been answered. Accidentally the first Pastbin has been overwritten. In case someone wanted to look at it.


I have been working on this quite some time and read through various posts here. I do not want to break the rules and look at other peoples solution.

So first the plaintext. I can not get my head around this yet. I have for instance 'A' which is the ASCII value of 65. And to encipher the code, I first have to encrypt my plaintext , to get the Alphabetical value of for example B or A which are 1 and 0.

So I get the plaintext with string p = GetString() and check with the function isupper(p) if there are any capital letters. So now, this is apparently not possible, since p is a string and not a char. So what I need to do is, iterate over the chars of p and subtract its ASCII value with the ASCII value of 'A' and the same process for islower with 'a'. Now the alphabetical value is available.

I did not find anything that helped to iterate over the chars of p. Can you give me a hint on how to do this ? Meaning getting the chars of p, since isupper(p) does not work.

OK. And what I ask myself now is, when I have the chars of p. I can initialize a for loop, that goes over the chars of p and print out the enciphered caesar code. With the caesar function c[i] = (p[i] + key) % 26.

Before this loop starts, I was using isalpha(p) which also causes a problem. Because it is a string it produces the segmentation fault. But why ?

So apparently, I want to grasp how to initiate a code or function to iterate over the plaintext p as chars.

There are many more questions. But I feel, I am missing something in my understanding. Of yeah something.

Here is the code http://pastebin.com/TQDeyQuY. It has been edited a lot because I was testing out the points, I explained above. And they did not work, for e.g checking if isupper(p) has capital letters and subtract 'A' or 65 from it. So what am I missing, any tips on how to iterate over the chars of p and to get the chars of p ?

Another question popped into my head, any tips on how to print out the enciphered text. But first a solution for iterating over, the chars of string p is very much appreciated!


Edit: The code is almost working now and I have a problem with check50. The code does not print additional character and spaces. That is why it is not passing check50. Now the idea came that when if (p[i] != isalpha(p[i])) printf("%c", p[i]); . So when p[i] is not in the alphabet print that character. Yet this does not work.

Now, the code has been changed and now it prints out each additional character. But it produces.

:( encrypts "BARFOO" as "EDUIRR" using 3 as key \ expected output, but not "BARFOO\n" :( encrypts "BaRFoo" as "FeVJss" using 4 as key \ expected output, but not "BeRFss\n"

Everything else in check50 is working. Going through the code, I could not identify the mistake. Why does this happen ? Here is the code in a new Pastbin http://pastebin.com/TQDeyQuY. I overwrote the old one, did not pay attention.

2

isupper() does not work on strings, it only works on chars, that's why you get the seg fault.

"I did not find anything that helped to iterate over the chars of p. Can you give me a hint on how to do this ? Meaning getting the chars of p, since isupper(p) does not work."

Simply use a for loop that iterates over the whole string p (which you've already done on line 22) and check every single car if isupper() or islower() and deal with each one individually

"any tips on how to print out the enciphered text."

You are going to encipher the text char by char. You first need to get a relation between the alphabetical order of the char and its order in the ASCII table (two relations actually, one for uppercase and one for lowercase). Once you've got it, you will need to perform some arthemtic operations on everycar in order to: 1st, get the alphabetical order, 2nd, add the key, 3rd, get the order in the ASCII table back again to print out.

Also, don't forget about the modulus operator (%) to wrap around the text. (I think Zamayla explained it very well)

| improve this answer | |
  • Almost finished the code now. Will check marked when the code is working. Thx for the comment was so fixed on iterating over str p instead of p[i]. Every character in a string is a char. Did not implement that lol. – Dimitri Sep 3 '16 at 22:22

You must log in to answer this question.

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