comp.lang.ada
 help / color / mirror / Atom feed
* help with string splitting
@ 2006-03-09  0:12 isaac2004
  2006-03-09  0:44 ` jimmaureenrogers
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: isaac2004 @ 2006-03-09  0:12 UTC (permalink / raw)


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

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;


   --Output the string PlainText to the screen

   for I in 1..(Length - 1) loop

      Put(Item => Plaintext (I));

   end loop;

   Close(File => Indata);

end;

the program just cuts off the last letter in the string. i think this
has something to do with my splitting of even number strings. any help
would be greatly appreciated.

Isaac




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  0:12 help with string splitting isaac2004
@ 2006-03-09  0:44 ` jimmaureenrogers
  2006-03-09  2:21 ` Jeffrey R. Carter
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: jimmaureenrogers @ 2006-03-09  0:44 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  0:12 help with string splitting isaac2004
  2006-03-09  0:44 ` jimmaureenrogers
@ 2006-03-09  2:21 ` Jeffrey R. Carter
  2006-03-09  5:54   ` isaac2004
  2006-03-09  7:43 ` Anders Wirzenius
  2006-03-09 10:15 ` Georg Bauhaus
  3 siblings, 1 reply; 11+ messages in thread
From: Jeffrey R. Carter @ 2006-03-09  2:21 UTC (permalink / raw)


isaac2004 wrote:
> 
>    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

What does it mean for Length to be zero, J to be < 0, or K < 1? It seems Length
and K should be Positive, and J, Natural.

>    while not End_Of_File(File => Indata) loop
> 
>       Get(
>          File => Indata,
>          Item => Codedtext (Length));
>       Length := Length + 1;
> 
>    end loop;

Now Length is the # of characters read + 1.

>    --Reconfigures the length to account for odd or even string lengths
> 
>    if Length REM 2 = 0 then
>       J := 0;
>    else
>       J := 1;
>    end if;

J := Length rem 2;

Do you really want to base this on the length of the input string, which is
Length - 1?

>    for I in 1..(Length - 1) loop
> 
>       Put(Item => Plaintext (I));
> 
>    end loop;

Put (Item => Plaintext (1 .. Length - 1) );

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail
59




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  2:21 ` Jeffrey R. Carter
@ 2006-03-09  5:54   ` isaac2004
  2006-03-09 16:50     ` Martin Krischik
  2006-03-09 19:28     ` Jeffrey R. Carter
  0 siblings, 2 replies; 11+ messages in thread
From: isaac2004 @ 2006-03-09  5:54 UTC (permalink / raw)


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;

this code confuses me
what is Code_Index and Codetext'Length

>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.

that is my mistake it is really Mteohr

>Now Length is the # of characters read + 1.
the reason for doing that is to loop through the string and reading all
char to length.

thanks for the help guys




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  0:12 help with string splitting isaac2004
  2006-03-09  0:44 ` jimmaureenrogers
  2006-03-09  2:21 ` Jeffrey R. Carter
@ 2006-03-09  7:43 ` Anders Wirzenius
  2006-03-09 10:15 ` Georg Bauhaus
  3 siblings, 0 replies; 11+ messages in thread
From: Anders Wirzenius @ 2006-03-09  7:43 UTC (permalink / raw)


"isaac2004" <isaac_2004@yahoo.com> writes:

You could put in some debugging code:
...
>    Codedtext : Message;   --input - string of coded text
>    Plaintext : Message;   --output - plain text message

Give some initial values to the strings:
   Codedtext : Message := ('1','2','3','4','5','6','7',others => ' ');
   Plaintext : Message := ('1','2','3','4','5','6','7',others => ' ');

Then, in the loops, 
change the relevant position in the input string to 'U' (like "used") and
display the strings in the loops, like:


> 
>    for I in 2..(Length - 1) loop
> 
>       if I REM 2 = 0 then
>          Plaintext(I + 1) := Codedtext(K);

           Codedtext (K) := 'U';

>          K := K + 1;
>       else
>          null;
>       end if;

   Ada.Text_IO.Put_Line ("every odd:  " & Plaintext (1..Length-1) & "|"
   & Codedtext (1..Length-1));


and similar code for the "even" loop

Good luck!

-- 
Anders



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  0:12 help with string splitting isaac2004
                   ` (2 preceding siblings ...)
  2006-03-09  7:43 ` Anders Wirzenius
@ 2006-03-09 10:15 ` Georg Bauhaus
  3 siblings, 0 replies; 11+ messages in thread
From: Georg Bauhaus @ 2006-03-09 10:15 UTC (permalink / raw)


isaac2004 wrote:

> an example is this
> Mother would be Mtoehr encrypted

How about a little rearrangement and some functional
decomposition (if you can afford to be a little wasteful
in the first draft):

procedure encrypt (s: Message) is

   type Confusion is array(Positive range s'Range) of Positive;

   function permute(s: Message) return Confusion;


   new_idx: constant Confusion := permute(s);

begin
   for k in s'Range loop
     <copy characters to their new positions via `new_idx`>
   end loop;
end encrypt;



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  5:54   ` isaac2004
@ 2006-03-09 16:50     ` Martin Krischik
  2006-03-09 18:31       ` isaac2004
  2006-03-09 19:28     ` Jeffrey R. Carter
  1 sibling, 1 reply; 11+ messages in thread
From: Martin Krischik @ 2006-03-09 16:50 UTC (permalink / raw)


isaac2004 wrote:

> 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;
> 
> this code confuses me
> what is Code_Index and Codetext'Length

Code_Index is a variable declared as part of for loop. C99 can do that as
well - finally ;-) . See:

http://en.wikibooks.org/wiki/Ada_Programming/Control#for_loop

Codetext'Length is the length of the array  - mind you I would have used
Codetext'Range - which is range of valid indices for Codetext. See:

http://en.wikibooks.org/wiki/Ada_Programming/Attributes/'Length
http://en.wikibooks.org/wiki/Ada_Programming/Attributes/'Range

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09 16:50     ` Martin Krischik
@ 2006-03-09 18:31       ` isaac2004
  2006-03-09 19:58         ` jimmaureenrogers
  2006-03-10 10:45         ` Martin Krischik
  0 siblings, 2 replies; 11+ messages in thread
From: isaac2004 @ 2006-03-09 18:31 UTC (permalink / raw)


>You could put in some debugging code:
the thing is that i know what is wrong with the code but i cant figure
out the algorithm

>Code_Index is a variable declared as part of for loop. C99 can do that as
well - finally ;-)

so Code_index would be like this

for Code_Index in Count loop

bla bla ;
end loop;

Codetext'Length is just a variable for length and codetext'range is one
for max index
im kinda confused




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09  5:54   ` isaac2004
  2006-03-09 16:50     ` Martin Krischik
@ 2006-03-09 19:28     ` Jeffrey R. Carter
  1 sibling, 0 replies; 11+ messages in thread
From: Jeffrey R. Carter @ 2006-03-09 19:28 UTC (permalink / raw)


isaac2004 wrote:

>>for Code_Index in Codetext'Length loop
> 
> this code confuses me
> what is Code_Index and Codetext'Length

Code_Index is the index variable for the for loop. You've done the same thing in 
your code, where you called it I. The problem is that this for loop has an 
error; "for I in 7 loop" is not a valid for loop. The writer probably meant 
Codetext'range.

If you aren't familiar with the array attributes 'First, 'Last, 'Length, and 
'range, then you really need to spend some time with your textbook. If they're 
not described and used there, spend some time here:

http://www.adaic.org/standards/95lrm/html/RM-3-6-2.html

Attributes are an important part of the language, and you don't really know Ada 
if you're not familiar and comfortable with the common ones.

-- 
Jeff Carter
"Crucifixion's a doddle."
Monty Python's Life of Brian
82



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09 18:31       ` isaac2004
@ 2006-03-09 19:58         ` jimmaureenrogers
  2006-03-10 10:45         ` Martin Krischik
  1 sibling, 0 replies; 11+ messages in thread
From: jimmaureenrogers @ 2006-03-09 19:58 UTC (permalink / raw)


isaac2004 wrote:
> >You could put in some debugging code:
> the thing is that i know what is wrong with the code but i cant figure
> out the algorithm
>
> >Code_Index is a variable declared as part of for loop. C99 can do that as
> well - finally ;-)
>
> so Code_index would be like this
>
> for Code_Index in Count loop
>
> bla bla ;
> end loop;
>
> Codetext'Length is just a variable for length and codetext'range is one
> for max index
> im kinda confused

Codetext'Length is the length of the Codetext array. It is the number
of elements
in the array.

Codetext'range is the range of index values for the array Codetext.

Jim Rogers




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: help with string splitting
  2006-03-09 18:31       ` isaac2004
  2006-03-09 19:58         ` jimmaureenrogers
@ 2006-03-10 10:45         ` Martin Krischik
  1 sibling, 0 replies; 11+ messages in thread
From: Martin Krischik @ 2006-03-10 10:45 UTC (permalink / raw)


Well I quickly put a description together for you:

http://en.wikibooks.org/wiki/Ada_Programming/Types/array#Array_Attributes

do follow some of the links - especialy the one about loops on arrays.

Martin




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2006-03-10 10:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-09  0:12 help with string splitting isaac2004
2006-03-09  0:44 ` jimmaureenrogers
2006-03-09  2:21 ` Jeffrey R. Carter
2006-03-09  5:54   ` isaac2004
2006-03-09 16:50     ` Martin Krischik
2006-03-09 18:31       ` isaac2004
2006-03-09 19:58         ` jimmaureenrogers
2006-03-10 10:45         ` Martin Krischik
2006-03-09 19:28     ` Jeffrey R. Carter
2006-03-09  7:43 ` Anders Wirzenius
2006-03-09 10:15 ` Georg Bauhaus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox