1 / 15

ITEC 320

ITEC 320. Lecture 9 String rehash. Review. Questions? Arrays Dynamic arrays How? Typed and anonymous Slices Copying / Assignment. String’s. What do you remember about them? Declaration? Input? Output?. Code. What is the difference between. s: String := “Hello World”; And

lynde
Télécharger la présentation

ITEC 320

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 320 Lecture 9 String rehash

  2. Review • Questions? • Arrays • Dynamic arrays • How? • Typed and anonymous • Slices • Copying / Assignment

  3. String’s • What do you remember about them? • Declaration? • Input? • Output?

  4. Code • What is the difference between s: String := “Hello World”; And s2: String(1..15);

  5. Issues: types must match sizes have to match Modification s: String := "Hi World"; begin put(s’first); put(s’last); s(4) := 'T'; put_line(s); s(4 .. 6) := "Ron"; put_line(s); s := ”Bye world"; put_line(s);

  6. Modification(2) • Combinations s: String := "HiMom!"; u: String(1 .. 10); ... -- s := u; -- Compileerror -- u := s; -- Compileerror s := "HiBob!"; -- Okay -- s := "HiBilly-Bob!"; -- Compileerror -- s := s & "!"; -- Compileerror u = s & "!!!";

  7. Loops • Why access character by character? for i in 1 .. s'length loop for i in s'first .. s'last loop for i in s'range loop

  8. Input • Common usage s: String(1..10); len: Natural begin while not end_of_file loop get_line(s, len); put_line(s(1..len)); end loop;

  9. Java • Reference versus value • Why do you think ADA is focused on values? String s; String t = "Mama!"; System.out.println(s); s = "today"; s = t;

  10. Declare • Want to resize a String? • Use same syntax as arrays get(size); declare s3: String(1..size); begin get_line(s3, size);

  11. Differences • What is the difference between put(v(1 .. len)); put(v);

  12. Power tools • Unbounded strings (Like java) with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Strings.Unbounded_Text_IO; use Ada.Strings.Unbounded_Text_IO; procedure showUnboundedis s: Unbounded_String := "first"; begin s := "different"; s := s & "!!"; put_line(s); -- different!! end showUnbounded

  13. Issues • Stack versus heap • Assignment with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; with Ada.Strings.Unbounded_Text_IO; use Ada.Strings.Unbounded_Text_IO; ... s: Unbounded_String := "first"; t: Unbounded_String := s; ... s := s & "!!"; put_line(s); -- first!! put_line(t) -- first;

  14. Strength/Weakness • Fixed length • Unconstrained • Unbounded type string is array (Positive range) <> of Character

  15. Summary • String’s are just like arrays • Slices make them tolerable • Don’t have the tools you have in Java

More Related