home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / STRINGS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  2.0 KB  |  114 lines

  1. // -------- strings.cpp
  2.  
  3. #include <iostream.h>
  4. #include <string.h>
  5. #include "strings.h"
  6.  
  7. // ------- put a string into a String
  8. void String::putstr(char *s)
  9. {
  10.     if (s == NULL)
  11.         sptr = NULL;
  12.     else    {
  13.         sptr = new char[strlen(s)+1];
  14.         strcpy(sptr, s);
  15.     }
  16. }
  17.  
  18. // ------- construct with a char * initializer
  19. String::String(char *s)
  20. {
  21.     putstr(s);
  22. }
  23.  
  24. // ------- construct with another String as initializer
  25. String::String(String& s)
  26. {
  27.     putstr(s.sptr);
  28. }
  29.  
  30. // -------- construct with just a size
  31. String::String(int len)
  32. {
  33.     sptr = new char[len+1];
  34.     memset(sptr, 0, len+1);
  35. }
  36.  
  37. // -------- assign a char array to a string
  38. String& String::operator=(char *s)
  39. {
  40.     if (sptr != NULL)
  41.         delete sptr;
  42.     putstr(s);
  43.     return *this;
  44. }
  45.  
  46. // ------- 1st concatenation operator (str1 += char *;)
  47. String& String::operator+=(char *s)
  48. {
  49.     char *sp = 
  50.         new char[(sptr ? strlen(sptr) : 0)+strlen(s)+1];
  51.     if (sptr != NULL)    {
  52.         strcpy(sp, sptr);
  53.         delete sptr;
  54.     }
  55.     else
  56.         *sp = '\0';
  57.     strcat(sp, s);
  58.     sptr = sp;
  59.     return *this;
  60. }
  61.  
  62. // ------ 3rd concatenation operator (str1 = str2 + char*;)
  63. String String::operator+(char *s)
  64. {
  65.     String tmp(*this);
  66.     tmp += s;
  67.     return tmp;
  68. }
  69.  
  70. // ------ 5th concatenation operator (str1 = char* + str2;)
  71. String operator+(char *s, String& s1)
  72. {
  73.     String tmp(s);
  74.     tmp += s1;
  75.     return tmp;
  76. }
  77.  
  78. // ------ substring: right len chars
  79. String String::right(int len)
  80. {
  81.     String tmp(sptr + strlen(sptr) - len);
  82.     return tmp;
  83. }
  84.  
  85. // ------ substring: left len chars
  86. String String::left(int len)
  87. {
  88.     String tmp(len+1);
  89.     strncpy(tmp.sptr, sptr, len);
  90.     return tmp;
  91. }
  92.  
  93. // ------ substring: middle len chars starting from where
  94. String String::mid(int len, int where)
  95. {
  96.     String tmp(len+1);
  97.     strncpy(tmp.sptr,sptr+where-1,len);
  98.     return tmp;
  99. }
  100.  
  101. ostream& operator<<(ostream& os, String& st)
  102. {
  103.     if (st.sptr != NULL) 
  104.         os << st.sptr;
  105.     return os;
  106. }
  107.  
  108. istream& operator>>(istream& is, String& st)
  109. {
  110.     if (st.sptr != NULL)
  111.         is >> st.sptr;
  112.     return is;
  113. }
  114.