home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / man / string.tex < prev    next >
Encoding:
Text File  |  1991-11-15  |  5.4 KB  |  150 lines

  1. \bigskip
  2. \bigskip
  3. {\magonebf 2.3 Strings (string)}     
  4.  
  5. Data type $string$ is the LEDA equivalent of $char*$ in \CC. The differences 
  6. to the $char*$-type are that assignment, compare  and concatenation 
  7. operators are defined and that argument passing by value works properly, 
  8. i.e., there is passed a copy of the string and not only a copy of a pointer.
  9. Furthermore a few useful operations for string manipulations are available. 
  10. The $\tilde{}$ operator converts a string instance to a $char*$.
  11.  
  12.  
  13. \def\name{$string$}
  14. \def\type{$string$}
  15.  
  16. {\bf 1. Creation of a string }
  17.  
  18. a) \create s {}
  19.  
  20. b) \create s (char*\ c)
  21.  
  22. introduces a variable $s$ of type $string$ initialized with the empty
  23. string. Variant b) takes as argument a string constant $c$ (char$*$) 
  24. and initializes $s$ with $c$. 
  25.  
  26.  
  27. \bigskip
  28. {\bf 2. Operations on a string s}
  29.  
  30. \medskip
  31. \+\cleartabs & \hskip 2.5truecm & \hskip 5truecm &\cr
  32. \+\op int     length {}
  33.                           {returns the length of string $s$}
  34. \smallskip
  35. \+\opa char\&   {int\ i}   
  36.                           {returns the character at position $i$}
  37. \+\nop                    {\precond{$0 \le i \le s$.length()-1} }
  38. \smallskip
  39. \+\opf string {int\ i,\ int\ j}
  40.                           {returns the substring of $s$ starting at}
  41. \+\nop                    {position $i$ and ending at position $j$}
  42. \+\nop                    {\precond{$0 \le i \le j \le s$.length()-1} }
  43. \smallskip
  44. \+\op string  tail   {int\ i}       
  45.                           {returns the last $i$ characters of $s$ }
  46. \smallskip
  47. \+\op string  head   {int\ i}       
  48.                           {returns the first $i$ characters of $s$ }
  49. \smallskip
  50.  
  51. \+\op int     pos    {string\ s_1}    
  52.                         {returns the first position of $s_1$ in $s$ if $s_1$ is}
  53. \+\nop                  {a substring of $s$, $-1$ otherwise}
  54. \smallskip
  55. \+\op int     pos    {string\ s_1,\ int\ i}    
  56.                           {returns the first position of $s_1$ in $s$ right of}
  57. \+\nop                    {position $i$ (-1 if no such position exists)}
  58. \smallskip
  59. \smallskip
  60. \+\op string  insert {string\ s_1,\ int\ i}
  61.                           {returns $s(0,i-1)$ + $s_1$ + $s(i+1,s.length())$}
  62. \+\nop                    {\precond{$0 \le i \le s$.length()-1} }
  63. \smallskip
  64. \+\op string  replace {string\ s_1,\ string\ s_2,\ int\ i=1} {}
  65. \+\nop                    { returns the string created from $s$ by replacing }
  66. \+\nop                    { the $i$-th occurence of $s_1$ in $s$ by $s_2$ }
  67. \smallskip
  68. \+\op string  replace\_all {string\ s_1,\ string\ s_2} {}
  69. \+\nop                    { returns the string created from $s$ by replacing }
  70. \+\nop                    { all occurences of $s_1$ in $s$ by $s_2$ }
  71. \vfill\eject
  72.  
  73. \+\op string  replace {int\ i,\ int\ j,\ string\ s_1} {}
  74. \+\nop                    { returns the string created from $s$ by replacing }
  75. \+\nop                    { $s(i,j)$ by $s_1$ }
  76. \smallskip
  77. \+\op string  replace {int\ i,\ string\ s_1} {}
  78. \+\nop                    { returns the string created from $s$ by replacing }
  79. \+\nop                    { $s[i]$ by $s_1$ }
  80. \smallskip
  81. \+\op string del {string\ s_1,\ int\ i=1}  
  82.                           { returns $s$.replace($s_1,"",i$) }
  83. \smallskip
  84. \+\op string del\_all {string\ s_1}         
  85.                           { returns $s$.replace\_all($s_1,""$) }
  86. \smallskip
  87. \+\op string del {int\ i,\ int\ j}        
  88.                           { returns $s$.replace($i,j,""$) }
  89. \smallskip
  90. \+\op string del {int\ i}                 
  91.                           { returns $s$.replace($i,""$) }
  92. \smallskip
  93. \+\op void   read {istream\  I,\ char\ delim = '\ '} {}
  94. \+\nop                    {reads characters from input stream $I$ into $s$}
  95. \+\nop                    {until the first occurence of character $delim$}
  96. \smallskip
  97. \+\op void   read {char\  delim = '\ '}  
  98.                           { read($cin$,$delim$) }
  99. \smallskip
  100. \+\op void   readline {istream\ I}       
  101.                           { read($I$,'$\backslash$n') }
  102. \smallskip
  103. \+\op void   readline {}                 
  104.                           { readline($cin$) }
  105. \smallskip
  106. \+\opb string\&  =  s_1  
  107.                           {assigns the value of $s_1$ to $s$ and returns $s$}
  108. \smallskip
  109. \+\opb string    +  s_1  
  110.                           {returns the concatenation of $s$ and $s_1$}
  111. \smallskip
  112. \+\opb string\&  += s_1  
  113.                           {appends $s_1$  to $s$ and returns $s$}
  114. \smallskip
  115. \+\opb bool      == s_1  
  116.                           {true iff $s$ and $s_1$ are equal}
  117. \smallskip
  118. \+\opb bool      != s_1  
  119.                           {true iff $s$ and $s_1$ are not equal}
  120. \smallskip
  121. \+\opb bool      <  s_1  
  122.                           {true iff $s$ is lexicographically smaller than $s_1$}
  123. \smallskip
  124. \+\opb bool      >  s_1  
  125.                           {true iff $s$ is lexicographically greater than $s_1$}
  126. \smallskip
  127. \+\opb bool      <= s_1  
  128.                           {returns $(s < s_1)\ ||\ (s == s_1)$}
  129. \smallskip
  130. \+\opb bool      >= s_1  
  131.                           {returns $(s > s_1)\ ||\ (s == s_1)$}
  132. \smallskip
  133. \+\ops ostream\&  <<  O  
  134.                           { writes string $s$ to the output stream $O$ }
  135. \smallskip
  136. \+\ops istream\&  >>  I  
  137.                           { read($I$,'\ ') }
  138. \smallskip
  139. \+\opu char*    \tilde{} 
  140.                           {converts $s$ into a \CC string ($char*$)}
  141.  
  142.  
  143. \bigskip
  144. {\bf 3. Implementation }
  145. \smallskip
  146. Strings are implemented by \CC character vectors. All operations on a
  147. string $s$ take time $O(s.length())$.
  148.  
  149.  
  150.