fork download
  1. // Author: Nicholas Dornelles Franceschini
  2. // Filename:
  3. // Date: September, 2014
  4. // Compiler: Quincy 2005 ver. 1.3
  5. // OS: Windows 7 SP1
  6. // Assigned: September 9th
  7. // Due: September 19th
  8. // Description: This program will mimic the cstring header
  9. // file, giving access to functions that will
  10. // count the characters in a string, compare
  11. // strings, copy one string onto another and
  12. // concatenate two strings.
  13. ///////////////////////////////////////////////////////////
  14.  
  15.  
  16. // System header files
  17. #include <iostream>
  18.  
  19.  
  20. // C-String Function Prototypes
  21. int ndfStrLen(const char * const);
  22. void ndfStrCpy(char * const, const char * const);
  23. void ndfStrCat(char [], const char []);
  24. int ndfStrCmp(const char [], const char []);
  25. void ndfOutLen(const char * const, const char * const);
  26.  
  27.  
  28. // control function
  29. int main()
  30. {
  31. char array1[100] = "lol";
  32. char array2[100] = "lol";
  33.  
  34. std::cout << "Please enter two strings. Hit enter after each one." << std::endl;
  35. std::cin.getline(array1, 99, '\n');
  36. std::cin.getline(array2, 99, '\n');
  37. std::cout << std::endl;
  38.  
  39. ndfOutLen(array1, array2);
  40. std::cout << std::endl;
  41.  
  42. ndfStrCmp(array1, array2);
  43. std::cout << std::endl;
  44.  
  45. ndfStrCpy(array2, array1);
  46. std::cout << array1 << std::endl;
  47. std::cout << array2 << std::endl;
  48. std::cout << std::endl;
  49.  
  50. ndfOutLen(array1, array2);
  51. std::cout << std::endl;
  52.  
  53. ndfStrCat(array2, array1);
  54. std::cout << "String \"" << array2 << "\" is " << ndfStrLen(array2) << " characters long." << std::endl;
  55.  
  56. ndfStrCmp(array1, array2);
  57. std::cout << std::endl;
  58.  
  59.  
  60. return 0;
  61. }
  62.  
  63.  
  64. ///////////////////////////////////////////////////////////
  65. // Purpose: This function counts the number of characters
  66. // in a string.
  67. // Input: One char array containing a string.
  68. // Output: An integer of value equal to the number of
  69. // characters, minus the null, in the provided
  70. // char array.
  71. int ndfStrLen(const char * const chArray1)
  72. {
  73. int counter = 0;
  74. char content = '\0';
  75. bool isNull = false;
  76.  
  77. // This loops through the char array until it finds a
  78. // null char. If it does not, it adds one to counter,
  79. // if it does, isNull becomes TRUE, closing the loop
  80. // and returning counter, which will contain a number
  81. // equivalent to the number of chars excluding the \0
  82. while(isNull == false)
  83. {
  84. content = *(chArray1 + counter);
  85. if(content == '\0')
  86. {
  87. isNull = true;
  88. }
  89. counter++;
  90. }
  91.  
  92. // returns the total number of chars in the array minus
  93. // the null char.
  94. return counter - 1;
  95. }
  96.  
  97. ///////////////////////////////////////////////////////////
  98. // Purpose: This function copies the second char array
  99. // into the first, overwriting what it originally
  100. // had in it and assuming it is big enough.
  101. // Input: Two char arrays.
  102. // Output: None, this function will edit the array.
  103. void ndfStrCpy(char * const chArray1, const char * const chArray2)
  104. {
  105. int counter = 0;
  106. char content = '\0';
  107. bool isNull = false;
  108.  
  109. // This loops through the second char array, copying
  110. // the char into the first array, until it finds a
  111. // null char. If it does not; it adds one to counter,
  112. // if it does; isNull becomes TRUE, closing the loop.
  113. while(isNull == false)
  114. {
  115. content = *(chArray2 + counter);
  116. *(chArray1 + counter) = content;
  117. if(content == '\0')
  118. {
  119. isNull = true;
  120. }
  121. counter++;
  122. }
  123.  
  124. return;
  125. }
  126.  
  127. ///////////////////////////////////////////////////////////
  128. // Purpose: This funtion adds the second char array to the
  129. // first one and assumes that there is enough
  130. // space for it.
  131. // Input: Two char arrays.
  132. // Output: None, this function will edit the array.
  133. void ndfStrCat(char chArray1[], const char chArray2[])
  134. {
  135. int counter = 0;
  136. char content = '\0';
  137. bool isNull = false;
  138. int length = 0;
  139.  
  140. // This figures out the size of the first array
  141. length = ndfStrLen(chArray1);
  142.  
  143. // This loops through both arrays, assigning chars from
  144. // second array to the end of the first array.
  145. while(isNull == false)
  146. {
  147. content = chArray2[counter];
  148. chArray1[length + counter] = content;
  149. if(content == '\0')
  150. {
  151. isNull = true;
  152. }
  153. counter++;
  154. }
  155.  
  156. return;
  157. }
  158.  
  159. ///////////////////////////////////////////////////////////
  160. // Purpose: This function will compare 2 char arrays.
  161. // Input: Two char arrays.
  162. // Output: An integer; positive if array 1 is bigger, 0 if
  163. // they are equal or negative is array 2 is bigger
  164. int ndfStrCmp(const char chArray1[], const char chArray2[])
  165. {
  166. int counter = 0;
  167. char contentArr1 = '\0';
  168. char contentArr2 = '\0';
  169. bool isDone = false;
  170. int result = 0;
  171.  
  172.  
  173. while(isDone == false)
  174. {
  175. contentArr1 = chArray1[counter];
  176. contentArr2 = chArray2[counter];
  177.  
  178. if(contentArr1 == '\0' || contentArr2 == '\0')
  179. {
  180. isDone = true;
  181. }
  182. else
  183. {
  184. result = contentArr1 - contentArr2;
  185.  
  186. if(result > 0)
  187. {
  188. isDone = true;
  189. std::cout << chArray1 << " is greater than " << chArray2 << "." << std::endl;
  190. }
  191. else if(result < 0)
  192. {
  193. isDone = true;
  194. std::cout << chArray1 << " is less than " << chArray2 << "." << std::endl;
  195. }
  196. }
  197. if(result == 0)
  198. {
  199. std::cout << "The strings are equal." << std::endl;
  200. }
  201. counter++;
  202. }
  203.  
  204. return result;
  205. }
  206.  
  207. ///////////////////////////////////////////////////////////
  208. // Purpose: This function prints both arrays and their
  209. // lengths.
  210. // Input: Two char arrays.
  211. // Output: Array and size to screen, nothing internally.
  212. void ndfOutLen(const char * const chArray1, const char * const chArray2)
  213. {
  214. std::cout << "String \"" << chArray1 << "\" is " << ndfStrLen(chArray1) << " characters long." << std::endl;
  215. std::cout << "String \"" << chArray2 << "\" is " << ndfStrLen(chArray2) << " characters long." << std::endl;
  216.  
  217. return;
  218. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Please enter two strings. Hit enter after each one.

String "" is 0 characters long.
String "" is 0 characters long.

The strings are equal.




String "" is 0 characters long.
String "" is 0 characters long.

String "" is 0 characters long.
The strings are equal.