Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdlib.h>
- //to do list:
- //1. make all lower case COMPLETED
- //2. remove spaces COMPLETED
- //3. allow for file insertion MOSTLY COMPLETED
- //4. Create flags to allow for manual or file insertion COMPLETED
- //5. Clean up main to only be the argument decisions everything else is functions COMPLETED
- //6. When tstBufStr goes into cleanstring it gets lost some how. line 48 COMPLETED
- int palintest(char testString[10], int slength);
- void palinoutput(int resultflag);
- char* cleanstring(char *passString);
- char* fileopen(char *str, int periodCount);
- int main(int argc, char *argv[]) {
- char testBuffer[1000];
- char finalBuffer[1000];
- int stringLength;
- int palinFlag;
- int countPeriods = 0;
- char *ptr = finalBuffer;
- char *tstBufPtr = testBuffer;
- switch (*argv[1]) {
- case 'i': //string passed through argument option
- if (strlen(argv[2]) >= 100) {
- printf("ERROR: String is too long\n");
- return 0;
- }
- //This is stupid but I wanted to do it anyway
- palinoutput(palintest(finalBuffer,
- strlen(strcpy(finalBuffer,
- cleanstring(strcpy(testBuffer, argv[2]))))));
- break;
- case 'f': //file open option
- for (int q = 0; q < 10; q++) {
- tstBufPtr = fileopen(argv[2], countPeriods);
- if (tstBufPtr == "<(^_^<)") //Exits program if kirby is in tstBufPtr
- return 0;
- strcpy(finalBuffer, tstBufPtr);
- cleanstring(finalBuffer);
- stringLength = strlen(finalBuffer);
- palinFlag = palintest(finalBuffer, stringLength);
- palinoutput(palinFlag);
- countPeriods++;
- }
- break;
- default:
- printf("Error no options selected\n");
- }
- return 0;
- }
- //Palindrome test function-------------------------------------------------------------------------
- int palintest(char testString[10], int slength) {
- int loopCount;
- int results = 1;
- slength--;
- for(loopCount = 0; loopCount < slength; loopCount++) {
- if (testString[loopCount] == testString[slength]) {
- slength--;
- }
- else {
- results = 0;
- loopCount = loopCount + slength;
- }
- /*Test the first and last characters against each other to see if they are
- the same value. If the string is an uneven number the loop ignores when
- loopCount and slength are equal so it is never tested and assumed true.
- The result flag is set to true and only made untrue by a failure in comparison
- */
- }
- return results;
- }
- //Output function **********************************
- void palinoutput(int resultflag) {
- switch(resultflag) { //Output to user
- case 0:
- printf("The string is not a palindrome\n\n");
- break;
- case 1:
- printf("The string is a palindrome\n\n");
- break;
- default:
- printf("Error");
- }
- }
- //Clean up string function***************************
- char* cleanstring(char *passString) {
- int count = 0;
- char makeLower[100];
- char fBuffer[100];
- int spaceCount = 0;
- int lineLength;
- lineLength = strlen(passString);
- for(count = 0; count <= lineLength; count++) {
- makeLower[count] = tolower(passString[count]); //Makes all char lowercase
- if (makeLower[count] != ' ') { //Deletes spaces
- passString[spaceCount] = makeLower[count];
- spaceCount++;
- }
- }
- return passString;
- }
- //file open function**********************************
- char* fileopen(char *str, int periodCount) {
- int stringLength = 0;
- static int iCurrent;
- static int iPrevious = 0;
- int holdBuffLoop = 0;
- static int i;
- int doLoop = 0;
- FILE *fp;
- //Maybe need ppointers to the char to make it work getting funky buffer reads
- //I am not super fond of this function overall I want to revisit it and clean it up
- char buffer[1000];
- char period[20];
- char holdBuffer[250];
- fp = fopen(str, "r");
- fread(buffer, 999, 1,fp);
- stringLength = strlen(buffer);
- if(i >= stringLength) {
- str = "<(^_^<)"; //janky ass way of exiting the function if we hit the end
- fclose(fp);
- return str;
- }
- strcpy(period, ".?"); //this can be improved some how
- if(periodCount == 0) {
- i = 0;
- }
- do {
- if (buffer[i] == period[0] || buffer[i] == period[1]) //this tests if we hit punctuation
- doLoop = 1; //There maybe a better way
- else if (i >= stringLength) //this seems super jank
- doLoop = 1;
- else
- i++;
- }while(doLoop != 1);
- if (periodCount != 0) {
- iPrevious = iCurrent + 1; //This exists to be able to set iPrevious once
- }
- else {
- iPrevious = 0;
- }
- iCurrent = i;
- memset(holdBuffer,0,strlen(holdBuffer)); //what the fuck does this do?
- holdBuffLoop = 0;
- for (iPrevious; iPrevious < iCurrent; ++iPrevious) {
- holdBuffer[holdBuffLoop] = buffer[iPrevious];
- holdBuffLoop++;
- }
- str = holdBuffer;
- printf("The string under test is: %s\n", str);
- i++;
- fclose(fp);
- return str;
- }
Add Comment
Please, Sign In to add comment