fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // get string length
  6. int StrLen(char a[]);
  7. // copy array b to array a
  8. void StrCopy(char a[], char b[]);
  9. // add array b to the end of array a
  10. void StrCat(char a[], char b[]);
  11. // compare array b to array a for a match
  12. int StrComp(char a[], char b[]);
  13. // converts string to uppercase
  14. void GoUpper(char a[]);
  15. // converts strig to lowercase
  16. void GoLower(char a[]);
  17. // string a is greater than string b
  18. bool StrGreat(char a[], char b[]);
  19. // string a is less than string b
  20. bool StrLess(char a[], char b[]);
  21. // string a is equal to string b
  22. bool StrEqual(char a[], char b[]);
  23. // find a single character in a string
  24. int Find(char myString[], char findThis, int pos);
  25. // find any character in the character set within the string
  26. int FindAny(char myString[], char findThese[], int pos);
  27. // find any character in the string that's not in the character set
  28. int FindNotAny(char myString[], char findThese[], int pos);
  29. // creates a new string from an exisiting string
  30. void SubStr(char myString[], char subString[], int start, int length);
  31. // requires lower level function: FindAny (gets the beginning index of the substring)
  32. // requires lower level function: FindNotAny (gets the ending index of the substring)
  33. // subtracting the value of FindNotAny from FindAny returns the length of the substring
  34. // requires lower level function: SubStr (passing the length (FindNotAny - FindAny = length) will produce the desired substring)
  35. // Zorg returns an integer which is the current index of the string
  36. int Zorg(char myString[], char charSet[], char subString[], int pos);
  37. // requires lower level function: Find (looks at the first character to determine it's type)
  38. // token types are a: letters, n: numbers, s: special, w: whitespace
  39. // requires lower level function: Zorg (creates tokens(substrings) and updates index
  40. bool GetToken(char Block[], char Token[], char &tokenType, int &pos);
  41. // token blocks
  42. void GetTokenizedBlock(char words[][50], char block[], char token[], char &tokenType, int &pos);
  43.  
  44. int main() {
  45. char block[1000] = "running in the woods.";
  46. char token[1000];
  47. char words[25][50];
  48. char tokenType;
  49. int pos = 0;
  50.  
  51. cout << block << endl;
  52.  
  53. GetTokenizedBlock(words, block, token, tokenType, pos);
  54.  
  55. cout << words[0] << endl;
  56. cout << words[1] << endl;
  57. cout << words[2] << endl;
  58. cout << words[3] << endl;
  59.  
  60. return 0;
  61. }
  62.  
  63. void GetTokenizedBlock(char words[][50], char block[], char token[], char &tokenType, int &pos) {
  64. // return all tokens
  65. while (GetToken(block, token, tokenType, pos)) {
  66. if (tokenType != 'w') {
  67. for(int a = 0; a < pos; a++) {
  68. StrCopy(words[a], token);
  69. }
  70. }
  71. }
  72. }
  73.  
  74. // function gettoken
  75. bool GetToken(char Block[], char Token[], char &tokenType, int &pos) {
  76. // token type details
  77. char alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  78. char numeric[] = "1234567890";
  79. char special[] = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
  80. char whitespace[] = "\t\n ";
  81.  
  82. // stop once the
  83. if (pos == -1) {
  84. return false;
  85. }
  86.  
  87. // determines the token type
  88. // searches token type string for a match(greater than -1)
  89. if (Find(alpha, Block[pos], 0) != -1) {
  90. // alpha type
  91. tokenType = 'a';
  92. // updated position
  93. pos = Zorg(Block, alpha, Token, pos);
  94. } else if (Find(numeric, Block[pos], 0) != -1) {
  95. // number type
  96. tokenType = 'n';
  97. // updated position
  98. pos = Zorg(Block, numeric, Token, pos);
  99. } else if (Find(special, Block[pos], 0) != -1) {
  100. // special type
  101. tokenType = 's';
  102. // updated position
  103. pos = Zorg(Block, special, Token, pos);
  104. } else if (Find(whitespace, Block[pos], 0) != -1) {
  105. // white space type
  106. tokenType = 'w';
  107. // updated position
  108. pos = Zorg(Block, whitespace, Token, pos);
  109. } else {
  110. return false;
  111. }
  112.  
  113. return true;
  114. }
  115.  
  116. // function zorg
  117. int Zorg(char myString[], char charSet[], char subString[], int pos) {
  118. // start of token
  119. int start = FindAny(myString, charSet, pos);
  120. // end of token
  121. int end = FindNotAny(myString, charSet, start);
  122. // update index for next token
  123. int length = end - start;
  124.  
  125. // makes a token(substring) from the string
  126. SubStr(myString, subString, start, length);
  127.  
  128. // the position in the string after the token is found
  129. return end;
  130. }
  131.  
  132. // substring function (takes a portion of a string and creates a new string)
  133. void SubStr(char myString[], char subString[], int start, int length) {
  134. // variable declaration
  135. int a = 0, // starting position for subString
  136. b = start; // starting position for myString
  137.  
  138. // while starting position is less than length
  139. while (a <= (length - 1) && b <= StrLen(myString)) {
  140. // each position b on myString is set to positions a on subString
  141. // as long as it is between start and length
  142. subString[a] = myString[b];
  143. // increment positions for myString and subString
  144. a++;
  145. b++;
  146. }
  147. // set null to subsring
  148. subString[a] = '\0';
  149. }
  150.  
  151. // function string length
  152. int StrLen(char a[]) {
  153. // variable declaration
  154. int i = 0; // index starts at 0
  155.  
  156. // while array a is now null (for each element in the array)
  157. while (a[i] != '\0') {
  158. // increment the index
  159. i++;
  160. }
  161. // returns the last index of the array
  162. return i;
  163. }
  164.  
  165. // function copy
  166. void StrCopy(char a[], char b[]) {
  167. // variable declaration
  168. int i = 0; // index starts at 0
  169.  
  170. // while array b is not null (for each element in array b)
  171. while (b[i] != '\0') {
  172. // add array b value to array a
  173. a[i] = b[i];
  174. // increment index
  175. i++;
  176. }
  177. // set array to null
  178. a[i] = '\0';
  179. }
  180.  
  181. // function concatenate
  182. void StrCat(char a[], char b[]) {
  183. // variable declaration
  184. int i = StrLen(a), // length of array a
  185. u = 0; // index for array b
  186.  
  187. // while array b is not null (for each element in array b)
  188. while (b[u] != '\0') {
  189. // add array b value to array a
  190. a[i] = b[u++];
  191. // increment index
  192. i++;
  193. }
  194. // set array a to null
  195. a[i] = '\0';
  196. }
  197.  
  198. // function compare
  199. int StrComp(char a[], char b[]) {
  200. // variable declaration
  201. int i = 0;
  202.  
  203. // a is equal to b and bot are
  204. while (a[i] == b[i] && a[i] != '\0' && b[i] != '\0') {
  205. i++;
  206. }
  207. // a is greater than b
  208. if (a[i] > b[i]) {
  209. return 1;
  210. }
  211. // a is less than b
  212. if (a[i] < b[i]) {
  213. return -1;
  214. }
  215.  
  216. return 0;
  217. }
  218.  
  219. // convert to uppercase
  220. void GoUpper(char a[]) {
  221. // variable declaration
  222. int i = 0;
  223.  
  224. while (a[i] != '\0') {
  225. // if characters are between a and z
  226. if (a[i] >= 'a' && a[i] <= 'z') {
  227. // subtract ' ' converts to upper
  228. a[i] -= ' ';
  229. // increment
  230. i++;
  231. }
  232. }
  233. }
  234.  
  235. // convert to lowercase
  236. void GoLower(char a[]) {
  237. // variable declaration
  238. int i = 0;
  239.  
  240. while (a[i] != '\0') {
  241. // if characters are between A and Z
  242. if (a[i] >= 'A' && a[i] <= 'Z') {
  243. // adding ' ' converts to lower
  244. a[i] += ' ';
  245. // increment
  246. i++;
  247. }
  248. }
  249. }
  250.  
  251. // greater than compare
  252. bool StrGreat(char a[], char b[]) {
  253. if (StrLen(a) > StrLen(b)) {
  254. return true;
  255. } else {
  256. return false;
  257. }
  258. }
  259.  
  260. // less than compare
  261. bool StrLess(char a[], char b[]) {
  262. if (StrLen(a) < StrLen(b)) {
  263. return true;
  264. } else {
  265. return false;
  266. }
  267. }
  268.  
  269. // equal compare
  270. bool StrEqual(char a[], char b[]) {
  271. if (StrLen(a) == StrLen(b)) {
  272. return true;
  273. } else {
  274. return false;
  275. }
  276. }
  277.  
  278. // find a key
  279. int Find(char myString[], char findThis, int pos) {
  280. // variable declaration
  281. int i = pos; // starting position
  282.  
  283. // loop string
  284. while (myString[i] != '\0') {
  285. // if match
  286. if (myString[i] == findThis) {
  287. return i;
  288. }
  289. // increment
  290. i++;
  291. }
  292.  
  293. return -1;
  294. }
  295.  
  296. // find any key
  297. int FindAny(char myString[], char findThese[], int pos) {
  298. // variable declaration
  299. int a = pos; // starting position
  300.  
  301. // loop string
  302. while (myString[a] != '\0') {
  303. // loop keys
  304. for (int b = 0; b < StrLen(findThese); b++) {
  305. if (myString[a] == findThese[b]) {
  306. return a;
  307. }
  308. }
  309. // increment myString position
  310. a++;
  311. }
  312. // return -1 if nothing is found
  313. return -1;
  314. }
  315.  
  316. // find not any function (look for anything as long as it's not part of findThese)
  317. int FindNotAny(char myString[], char findThese[], int pos) {
  318. // variable declaration
  319. int a = pos; // starting position for myString
  320. bool keeper; // keep track if anything is found
  321.  
  322. // loop myString
  323. while (myString[a] != '\0') { // while myString is not null
  324. //
  325. keeper = false;
  326. // loop findThese and compare to myString for a match and return true
  327. for (int b = 0; b < StrLen(findThese); b++) {
  328.  
  329. if (myString[a] == findThese[b]) {
  330.  
  331. keeper = true;
  332. }
  333. }
  334. //
  335. if (keeper != true) {
  336. return a;
  337. }
  338. // increment myString position
  339. a++;
  340. }
  341. // return -1 if nothing is found
  342. return -1;
  343. }
  344.  
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
running in the woods.
woods
woods
woods
woods