• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

the out put is wrong in my encryption code

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//this is the out put
pleas ente your plain text
aaa
pleas ente your key
3
the chiper text is:def


//please help me
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is that different from what you expect? It looks to be exactly what the code says to do.

p.s. When posting code, please follow some common indentation patterns. Your code is a mess, very hard to read, because it is not clear where one block ends and another starts. For a guide that will help make your code readable, see this style guide
 
hadeel marghlany
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
am sorry its my first time posting a code and my professers never told me this.
am telling the program

for(int i=0; i< message.length() ;i++){
for example aaa is my password
and here i=0 so 0<3

current=str.charAt(i);
so the current =0

for(pos=0;pos<25;pos++){
and posstion =0

if(current== str.charAt(pos))
and here if current witsh is 0 = possition witsh is 0 he will enter the if otherwise it will restart the loop

{chipher+=str.charAt((key+pos)%26);
lets say that i chose the key to be 3 so 3+0=3 witsh is d
and it should repeat this 3 times so ddd
so what do you mean by its doning what am asking it to do please explain more
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hadeel marghlany wrote:am sorry its my first time posting a code and my professers never told me this.
am telling the program

for(int i=0; i< message.length() ;i++){
for example aaa is my password
and here i=0 so 0<3

current=str.charAt(i);
so the current =0


What the for loop followed by the assignment means is 'for each character in the message, get the character at the same position in the str variable.

So current does not (or at any point) equal 0. In the first iteration, current is the character at position 0 in str, which is 'a'. Then the second time through the loop, it becomes 'b' and the final time it becomes 'c' (because you increment i).

for(pos=0;pos<25;pos++){
and posstion =0

if(current== str.charAt(pos))
and here if current witsh is 0 = possition witsh is 0 he will enter the if otherwise it will restart the loop


The for loop followed by the if statement says 'navigate through the characters in the str string. We only want to work when we get the character that is the same as current. Since current is 'a' we get to 'a' in the str string, (which is position 0) and then continue. When the outer loop progresses to its second iteration, current becomes 'b' so this inner for loop skips through the str string until it finds 'b' and pos become 1. Then the third iteration of the outer loop makes current = 'c', you skip through str until you find 'c' and pos becomes 2.

{chipher+=str.charAt((key+pos)%26);
lets say that i chose the key to be 3 so 3+0=3 witsh is d


This is actually the the key + the offset in the str where current is found (as found in the pos variable). The first time through the loop, current is 'a', so position is '0' and the chipher gets the character at 3 + 0, which is 'd'. The second time through the loop current is 'b', position is 1, so the chipher gets 3+1 which is 'e'. Finally, the last time through the outer loop current is 'c', position is '2', and chipher gets 3+2 which is 'f'.

and it should repeat this 3 times so ddd
so what do you mean by its doning what am asking it to do please explain more


So it does what it says. But not what you want. For example, when I first read your code I expected "abc" to produce "def" which it does. But this is a coincidence. You are actually using the wrong String to get the current value from - you are getting current from str, not the user input. So if the user input "rm5" you would still get "def" as the result.
 
hadeel marghlany
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did it thankyou so mush for your help ^_^
 
reply
    Bookmark Topic Watch Topic
  • New Topic