Guest User

Untitled

a guest
Nov 18th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <stdlib.h>
  7.  
  8. //to do list:
  9. //1. make all lower case COMPLETED
  10. //2. remove spaces COMPLETED
  11. //3. allow for file insertion MOSTLY COMPLETED
  12. //4. Create flags to allow for manual or file insertion COMPLETED
  13. //5. Clean up main to only be the argument decisions everything else is functions COMPLETED
  14. //6. When tstBufStr goes into cleanstring it gets lost some how. line 48 COMPLETED
  15.  
  16. int palintest(char testString[10], int slength);
  17. void palinoutput(int resultflag);
  18. char* cleanstring(char *passString);
  19. char* fileopen(char *str, int periodCount);
  20.  
  21. int main(int argc, char *argv[]) {
  22.  
  23. char testBuffer[1000];
  24. char finalBuffer[1000];
  25. int stringLength;
  26. int palinFlag;
  27. int countPeriods = 0;
  28. char *ptr = finalBuffer;
  29. char *tstBufPtr = testBuffer;
  30.  
  31. switch (*argv[1]) {
  32. case 'i': //string passed through argument option
  33. if (strlen(argv[2]) >= 100) {
  34.  
  35. printf("ERROR: String is too long\n");
  36. return 0;
  37.  
  38. }
  39.  
  40. //This is stupid but I wanted to do it anyway
  41.  
  42. palinoutput(palintest(finalBuffer,
  43. strlen(strcpy(finalBuffer,
  44. cleanstring(strcpy(testBuffer, argv[2]))))));
  45.  
  46. break;
  47. case 'f': //file open option
  48. for (int q = 0; q < 10; q++) {
  49. tstBufPtr = fileopen(argv[2], countPeriods);
  50. if (tstBufPtr == "<(^_^<)") //Exits program if kirby is in tstBufPtr
  51. return 0;
  52. strcpy(finalBuffer, tstBufPtr);
  53. cleanstring(finalBuffer);
  54. stringLength = strlen(finalBuffer);
  55. palinFlag = palintest(finalBuffer, stringLength);
  56. palinoutput(palinFlag);
  57. countPeriods++;
  58. }
  59. break;
  60. default:
  61. printf("Error no options selected\n");
  62. }
  63.  
  64. return 0;
  65. }
  66.  
  67. //Palindrome test function-------------------------------------------------------------------------
  68.  
  69. int palintest(char testString[10], int slength) {
  70.  
  71. int loopCount;
  72. int results = 1;
  73.  
  74. slength--;
  75. for(loopCount = 0; loopCount < slength; loopCount++) {
  76.  
  77. if (testString[loopCount] == testString[slength]) {
  78.  
  79. slength--;
  80.  
  81. }
  82. else {
  83.  
  84. results = 0;
  85. loopCount = loopCount + slength;
  86.  
  87. }
  88.  
  89. /*Test the first and last characters against each other to see if they are
  90. the same value. If the string is an uneven number the loop ignores when
  91. loopCount and slength are equal so it is never tested and assumed true.
  92.  
  93. The result flag is set to true and only made untrue by a failure in comparison
  94. */
  95. }
  96. return results;
  97. }
  98.  
  99. //Output function **********************************
  100.  
  101. void palinoutput(int resultflag) {
  102.  
  103. switch(resultflag) { //Output to user
  104. case 0:
  105. printf("The string is not a palindrome\n\n");
  106. break;
  107. case 1:
  108. printf("The string is a palindrome\n\n");
  109. break;
  110. default:
  111. printf("Error");
  112. }
  113.  
  114. }
  115.  
  116. //Clean up string function***************************
  117.  
  118. char* cleanstring(char *passString) {
  119.  
  120. int count = 0;
  121. char makeLower[100];
  122. char fBuffer[100];
  123. int spaceCount = 0;
  124. int lineLength;
  125. lineLength = strlen(passString);
  126.  
  127. for(count = 0; count <= lineLength; count++) {
  128.  
  129. makeLower[count] = tolower(passString[count]); //Makes all char lowercase
  130.  
  131. if (makeLower[count] != ' ') { //Deletes spaces
  132.  
  133. passString[spaceCount] = makeLower[count];
  134. spaceCount++;
  135.  
  136. }
  137.  
  138. }
  139. return passString;
  140. }
  141.  
  142. //file open function**********************************
  143.  
  144. char* fileopen(char *str, int periodCount) {
  145.  
  146. int stringLength = 0;
  147. static int iCurrent;
  148. static int iPrevious = 0;
  149. int holdBuffLoop = 0;
  150. static int i;
  151. int doLoop = 0;
  152.  
  153. FILE *fp;
  154. //Maybe need ppointers to the char to make it work getting funky buffer reads
  155. //I am not super fond of this function overall I want to revisit it and clean it up
  156. char buffer[1000];
  157. char period[20];
  158. char holdBuffer[250];
  159.  
  160. fp = fopen(str, "r");
  161. fread(buffer, 999, 1,fp);
  162. stringLength = strlen(buffer);
  163.  
  164. if(i >= stringLength) {
  165. str = "<(^_^<)"; //janky ass way of exiting the function if we hit the end
  166. fclose(fp);
  167. return str;
  168. }
  169.  
  170. strcpy(period, ".?"); //this can be improved some how
  171.  
  172. if(periodCount == 0) {
  173. i = 0;
  174. }
  175.  
  176. do {
  177. if (buffer[i] == period[0] || buffer[i] == period[1]) //this tests if we hit punctuation
  178. doLoop = 1; //There maybe a better way
  179. else if (i >= stringLength) //this seems super jank
  180. doLoop = 1;
  181. else
  182. i++;
  183. }while(doLoop != 1);
  184.  
  185. if (periodCount != 0) {
  186. iPrevious = iCurrent + 1; //This exists to be able to set iPrevious once
  187. }
  188. else {
  189. iPrevious = 0;
  190. }
  191. iCurrent = i;
  192. memset(holdBuffer,0,strlen(holdBuffer)); //what the fuck does this do?
  193. holdBuffLoop = 0;
  194.  
  195. for (iPrevious; iPrevious < iCurrent; ++iPrevious) {
  196.  
  197. holdBuffer[holdBuffLoop] = buffer[iPrevious];
  198. holdBuffLoop++;
  199.  
  200. }
  201. str = holdBuffer;
  202. printf("The string under test is: %s\n", str);
  203. i++;
  204. fclose(fp);
  205. return str;
  206.  
  207. }
  208.  
Add Comment
Please, Sign In to add comment