From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c8faa961aaf3fddb X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!e56g2000cwe.googlegroups.com!not-for-mail From: "jimmaureenrogers@worldnet.att.net" Newsgroups: comp.lang.ada Subject: Re: help with string splitting Date: 8 Mar 2006 16:44:39 -0800 Organization: http://groups.google.com Message-ID: <1141865079.012815.10520@e56g2000cwe.googlegroups.com> References: <1141863131.984654.144550@j52g2000cwj.googlegroups.com> NNTP-Posting-Host: 69.170.89.0 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1141865090 9196 127.0.0.1 (9 Mar 2006 00:44:50 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 9 Mar 2006 00:44:50 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: e56g2000cwe.googlegroups.com; posting-host=69.170.89.0; posting-account=SqOfxAwAAAAkL81YAPGH1JdBwpUXw9ZG Xref: g2news1.google.com comp.lang.ada:3298 Date: 2006-03-08T16:44:39-08:00 List-Id: isaac2004 wrote: > hello how would I create an Ada program to take an encrypted string > from a txt file and decrypt it. i have a so so program but it doesnt > work i cant get the algorithm right. > > an example is this > Mother would be Mtoehr encrypted > > so its just like cutting the string in half and rearranging the > chaacters to fix the decryption > > my example uses a txt file which the contents are > > encrypted version > aaabbb > > real code > ababab Your two examples above do not agree. Following the encrypted example, Mother would encrypt to Mteohr. I suspect that may have been a slip of the fingers during typing, but it does add confusion to your requirements. > > here is the program i have so far > > with Ada.Text_Io; > use Ada.Text_Io; > procedure Assignment_4 is > --------------------------------------------------------- > --|This program takes in a railfence encryption and deciphers it > --|Author: Isaac Levin > ------------------------------------------------------- > subtype Message is String (1..1024); > > Codedtext : Message; --input - string of coded text > Plaintext : Message; --output - plain text message > Indata : File_Type; > > Length : Natural := 1; --total size of the encrypted message > J : Integer := 0; --adjusts midpoint for odd and even strings > K : Integer := 2; --counter for the encrypted string > > begin -- Assingment_4 > > --Open the proper text file and read the code into the string > CodedText > > Open( > File => Indata, > Mode => In_File, > Name => "test.txt"); > > while not End_Of_File(File => Indata) loop > > Get( > File => Indata, > Item => Codedtext (Length)); > Length := Length + 1; > > end loop; > > --Reconfigures the length to account for odd or even string lengths > > if Length REM 2 = 0 then > J := 0; > else > J := 1; > end if; > > --Stores the first charcter of the coded message into the decoded > one > > if Length > 1 then > Plaintext(1) := Codedtext(1); > end if; > > --Stores every character of the encoded string up to the halfway > point > --into every odd position in the decoded one > > for I in 2..(Length - 1) loop > > if I REM 2 = 0 then > Plaintext(I + 1) := Codedtext(K); > K := K + 1; > else > null; > end if; > > end loop; > > K := 1; > > --Stores every character of the encoded string past the halfway > point > --into every even position in the decoded one > > for I in 2..(Length - 1) loop > > if I REM 2 = 0 then > Plaintext(I) := Codedtext((Length/2) + J + K); > K := K + 1; > else > null; > end if; > > end loop; > In both loops you are placing characters only into even positions in Plaintext. What about the odd positions? It also appears that you can do all the work in a single loop, cutting the time in half during which you traverse the strings. First_Half_Index : Positive := 1; Second_Half_Index : Positive := 2; for Code_Index in Codetext'Length loop if Code_Index <= Codetext'Length / 2 then Plaintext(First_Half_index) := Codetext(Code_Index); First_Half_Index := First_Half_Index + 2; else Plaintext(Second_Half_Index) := Codetext(Code_Index); Second_Half_Index := Second_Half_Index + 2; end if; end loop; I am not sure how the encryption deals with odd sized strings. Does the First Half or the Second Half get the extra character? Adjust the algorithm above accordingly. Jim Rogers