Advertisement
Guest User

str lib

a guest
Oct 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #pragma once
  2. #include "CS_Str.h"
  3. #include <iostream>
  4.  
  5. //Constructer
  6.  
  7. CS_Str::CS_Str()
  8. {
  9. char * nChar = nullptr;
  10. char * filler = nullptr;
  11.  
  12. }
  13.  
  14. // destructor
  15. CS_Str::~CS_Str()
  16. {
  17.  
  18. if (nChar != nullptr)
  19. {
  20. delete nChar;
  21. }
  22. else
  23. {
  24.  
  25.  
  26. }
  27. if (filler != nullptr)
  28. {
  29. delete[] filler;
  30. }
  31. else
  32. {
  33.  
  34.  
  35. }
  36.  
  37. filler = nChar = nullptr;
  38. }
  39.  
  40. // string constructor
  41.  
  42. CS_Str::CS_Str(char *nString)
  43. {
  44.  
  45. char* string1 = new char[strlen(nString)+1];
  46. string1 = nString;
  47.  
  48. }
  49.  
  50.  
  51.  
  52. //FUNCTION strlen(string)
  53. //SET character_index to 0
  54. //WHILE string[character_index] != 0
  55. //character_index++
  56. //ENDWHILE
  57. //RETURN character_index
  58. //ENDFUNCTION
  59.  
  60. int CS_Str::CS_strlen(char* nString)
  61. {
  62. if(nString == nullptr)
  63. {
  64. return -42;
  65. }
  66. int char_index = 0;
  67.  
  68. while (nString[char_index] != 0)
  69. {
  70. char_index++;
  71. }
  72. return char_index;
  73. }
  74.  
  75.  
  76.  
  77.  
  78. //FUNCTION strcpy(destination, source)
  79. //SET destination_index to strlen(destination) + 1
  80. //SET source_index to 0
  81. //WHILE source[source_index] != 0
  82. //SET destination[destination_index] to source[source_index]
  83. //++destination_index
  84. //++source_index
  85. //ENDWHILE
  86. //SET destination[destination_index] to 0
  87. //RETURN destination
  88. //ENDFUNCTION
  89.  
  90.  
  91. char CS_Str::CS_strcat(char *nString, char *wString)
  92. {
  93.  
  94. if (nString == nullptr)
  95. {
  96. return -42;
  97. }
  98. else if (wString == nullptr)
  99. {
  100. return -43;
  101. }
  102.  
  103. int wString_index = CS_strlen(wString) + 1;
  104. int nString_index = 0;
  105. while (nString[nString_index] != NULL)
  106. {
  107. wString[wString_index] = nString[nString_index];
  108. ++wString_index;
  109. ++nString_index;
  110. }
  111. wString[wString_index] = 0;
  112.  
  113.  
  114. return *nString;
  115. }
  116.  
  117. // String prepend, reverse append
  118. char CS_Str::CS_strpre(char *nString, char *wString)
  119. {
  120.  
  121. if (nString == nullptr)
  122. {
  123. return -44;
  124. }
  125. else if (wString == nullptr)
  126. {
  127. return -45;
  128. }
  129.  
  130. int wString_index = CS_strlen(wString) + 1;
  131. int nString_index = 0;
  132. while (wString[wString_index] != NULL)
  133. {
  134. nString[nString_index] = wString[wString_index];
  135. ++wString_index;
  136. ++nString_index;
  137. }
  138. wString[wString_index] = 0;
  139.  
  140.  
  141. return *nString;
  142. }
  143.  
  144.  
  145.  
  146. //FUNCTION strcmp(string1, string2)
  147. //SET character_index to 0
  148. //SET result to 0
  149. //SET running to true
  150. //WHILE running
  151. //IF string1[character_index] == 0 AND string2[character_index] == 0 THEN
  152. //SET result to 0
  153. //BREAK
  154. //ELSEIF string1[character_index] > string2[character_index] THEN
  155. //SET result to 1
  156. //BREAK
  157. //ELSE IF string1[character_index] < string2[character_index] THEN
  158. //SET result to - 1
  159. //BREAK
  160. //ELSE
  161. //++character_index
  162. //ENDIF
  163. //ENDWHILE
  164. //RETURN result
  165. //ENDFUNCTION
  166.  
  167. int CS_Str::CS_strcmp(char* nString, char* wString)
  168. {
  169.  
  170. if (nString == nullptr)
  171. {
  172. return -42;
  173. }
  174. else if (wString == nullptr)
  175. {
  176. return -43;
  177. }
  178.  
  179. int char_index = 0, result = 0;
  180. bool running = true;
  181. while (running = true)
  182. {
  183. if (nString[char_index] == 0 && wString[char_index] == 0)
  184. {
  185.  
  186. result = 0;
  187. break;
  188. }
  189. else if (nString[char_index] > wString[char_index])
  190. {
  191. result = 1;
  192. break;
  193. }
  194. else if (nString[char_index] < wString[char_index])
  195. {
  196. result = -1;
  197. break;
  198.  
  199. }
  200. else
  201. {
  202. ++char_index;
  203. }
  204. }
  205.  
  206. return result;
  207.  
  208. }
  209.  
  210. //FUNCTION strcpy(destination, source)
  211. //SET character_index to 0
  212. //WHILE source[character_index] != 0
  213. //SET destination[character_index] to source[character_index]
  214. //++character_index
  215. //ENDWHILE
  216. //SET destination[character_index] to 0
  217. //RETURN destination
  218. //ENDFUNCTION
  219.  
  220. char CS_Str::CS_strcpy(char *nString, char *wString)
  221. {
  222. if (nString == nullptr)
  223. {
  224. return -42;
  225. }
  226. else if (wString == nullptr)
  227. {
  228. return -43;
  229. }
  230.  
  231.  
  232. int char_index = 0;
  233. while (nString[char_index] != 0)
  234. {
  235. wString[char_index] = nString[char_index];
  236. ++char_index;
  237. }
  238. wString[char_index] = 0;
  239.  
  240.  
  241. return *wString;
  242.  
  243. }
  244.  
  245. //6. Write a function for each of the following descriptions.For each function, use the pointer
  246. //notation ONLY.Do NOT use the array index[] notation.
  247. //A.Write a function RevString(char* array) which reverses array.The function
  248. //returns nothing.
  249.  
  250.  
  251. //B.Write a function CountEven(int* array, int array_len) which receives an
  252. //integer array and its size, and returns the number of even numbers in the array.
  253.  
  254.  
  255. //C.Write a function Maximum(double* array, int array_size) that returns a
  256. //pointer to the maximum value of an array of doubles.If the array is empty, return
  257. //nullptr.
  258.  
  259.  
  260. //D.Write a function Contains(char* array, char search_value) which returns
  261. //true if the 1st parameter contains the 2nd parameter, or false otherwise.
  262.  
  263.  
  264. //Upper
  265.  
  266. char CS_Str::CS_strupper(char *nString)
  267. {
  268. if (nString == nullptr)
  269. {
  270. return -42;
  271. }
  272. int len = CS_strlen(nString);
  273. filler = new char[len + 1];
  274.  
  275.  
  276. for(int i = 0; i != len; i++)
  277. {
  278. if (filler[i] >= 95)
  279. {
  280. filler[i] = (nString[i] - 32);
  281. }
  282. else if ((nString[i] < 95))
  283. {
  284. filler[i] = nString[i] ;
  285. }
  286. else
  287. {
  288. return -47;
  289. }
  290.  
  291.  
  292. }
  293.  
  294. return *filler;
  295.  
  296. }
  297.  
  298. //Lower
  299.  
  300. char CS_Str::CS_strlower(char *nString)
  301. {
  302. if (nString == nullptr)
  303. {
  304. return -42;
  305. }
  306.  
  307. int len = CS_strlen(nString);
  308. filler = new char[len+1];
  309.  
  310. for (int i = 0; i <= len; i++ )
  311. {
  312. if (nString[i] >= 95)
  313. {
  314.  
  315. filler[i] = nString[i] ; // 204???
  316.  
  317.  
  318. }
  319. else if((int)nString[i] <= 95)
  320. {
  321.  
  322. filler[i] = ((int)nString[i] + 32); //40?? low upper, high lower ASCHII
  323.  
  324. }
  325. else
  326. {
  327. return -44;
  328. }
  329.  
  330. }
  331.  
  332. return *filler;
  333.  
  334. }
  335.  
  336.  
  337. //sub string index
  338.  
  339. //substring
  340.  
  341. //replace substring
  342.  
  343. //input c style??
  344.  
  345. //char index
  346.  
  347. char CS_Str::CS_charIndex(char *nString, int index)
  348. {
  349. if (nString == nullptr)
  350. {
  351. return 42;
  352. }
  353. else if (index > CS_strlen(nString))
  354. {
  355.  
  356. return 35;
  357.  
  358. }
  359.  
  360. nChar = new char;
  361. *nChar = nString[index-1];
  362. return *nChar;
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement