/* ========================================================================== */ /* */ /* string.c */ /* (c) April 10, 2017 K. J. Rock */ /* */ /* Description */ /* string manipulation */ /* */ /* ========================================================================== */ #include #include #include int main (void) { FILE *fp; int size, i=0; char *ptr, *ret, c; // working pointers and char container char *raven, *holmes, *tom, *huck; // pointers to our four strings //////////////////////////////////////////////////////////////////////////////// fp = fopen("The Raven.txt","r"); // open the file fseek(fp, 0L, SEEK_END); // find the end of the file size = ftell(fp); // save its size for memory space allocation rewind(fp); // rewind back to the beginning of the file raven = (char *) malloc(sizeof(char) * size); // allocate space for text // this line reads the file one character at a time // and saves it into raven as one long string while ((raven[i++] = fgetc(fp)) != EOF ) { } // read file until it ends // puts(raven); // print string in one long chunk fclose(fp); // close file. pointer ready for reuse //////////////////////////////////////////////////////////////////////////////// fp = fopen("The Memoirs of Sherlock Holmes.txt","r"); fseek(fp, 0L, SEEK_END); // find the end of the file size = ftell(fp); // save its size for space allocation rewind(fp); // fp back to beginning holmes = (char *) malloc(sizeof(char) * size); // allocate space for text i=0; while ((holmes[i++] = fgetc(fp)) != EOF ) { } // read file until it ends // puts(holmes); // print string in one long chunk fclose(fp); //////////////////////////////////////////////////////////////////////////////// fp = fopen("Huck Finn.txt","r"); fseek(fp, 0L, SEEK_END); // find the end of the fil size = ftell(fp); // save its size for space allocation rewind(fp); // fp back to beginning huck = (char *) malloc(sizeof(char) * size); // allocate space for text i=0; while ((huck[i++] = fgetc(fp)) != EOF ) { } // read file until it ends // puts(huck); // print string in one long chunk fclose(fp); //////////////////////////////////////////////////////////////////////////////// fp = fopen("Tom Sawyer.txt","r"); fseek(fp, 0L, SEEK_END); // find the end of the file size = ftell(fp); // save its size for space allocation rewind(fp); // fp back to beginning tom = (char *) malloc(sizeof(char) * size); // allocate space for text i=0; while ((tom[i++] = fgetc(fp)) != EOF ) { } // read file until it ends // puts(tom); // print string in one long chunk fclose(fp); //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // define string array dest and instantiate it with an end of string '\0' // find the first instance of 'x' in holmes // copy from that point for 20 characters into dest // print some whitespace and then the copied string //char dest[25]; // either use this line or the next two lines char *dest; dest = (char *) malloc(sizeof(char) * 25); memset(dest, '\0', sizeof(dest)); ret = strchr(holmes, 'x'); strncpy(dest, ret, 20); printf("\n\n"); puts(dest); free(dest); //////////////////////////////////////////////////////////////////////////////// // find the last instance of 'x' in holmes // print some whitespace and then print from the pointer to '\0' ret = strrchr(holmes, 'x'); printf("\n\n"); puts(ret); printf("\n\n"); //////////////////////////////////////////////////////////////////////////////// // need const because you are assigning before compile // these strings CANNOT be changed within the app // That makes it easy to create them but also makes them // incapable of change once created. Thus the const in the type. const char *dogs[] = {"Basset", "Schnauzer","Beagle","Standard Poodle", \ "Airedale", "Akita","Afghan","Dachshund","Dalmatian","Newfoundland"}; int rt; rt = strcmp(dogs[4], dogs[8]); if (rt) printf("%s is less than %s",dogs[4],dogs[8]); else printf("%s is less than %s",dogs[8],dogs[4]); puts("\n"); puts(dogs[9]); // display a few breeds puts(dogs[4]); puts(dogs[3]); puts(dogs[6]); puts(dogs[8]); puts("\n"); for (int k=0; k<10; k++) puts(dogs[k]); // display all of the breeds puts("\n"); // free(dogs); // woo hoo! throws interesting warning though // stacks are not heaps //////////////////////////////////////////////////////////////////////////////// // find Huck in the file tom const char needle[] = "Huck"; dest = (char *) malloc(sizeof(char) * sizeof(needle)); memset(dest, '\0', sizeof(dest)); ret = strstr(tom, needle); strncpy(dest, ret, sizeof(needle)); printf("The substring is: %s at %p\n", dest, ret); // location in haystack printf("\n\n"); free(dest); //////////////////////////////////////////////////////////////////////////////// // scan the entire string for all instances of the user's chosen word // show where each one is in memory // and display how many times the word was found in the string. int count=0; char *word; word = (char *) malloc(sizeof(char) * 35); // space for largish word printf("What's the word? => "); scanf("%s", word); ptr = tom; // point to the string of choice. while ((ret = strstr(ptr, word)) != NULL) { ++count; printf("The substring is: %s at %p\n", word, ret); ptr = ret + sizeof(word); // stop looking at same spot!! } puts("\n"); printf("Found '%s' %d times\n", word, count); printf("\n\n"); free(word); //////////////////////////////////////////////////////////////////////////////// // modify this code to determine the frequency // of all characters between a-z & A-Z // that is 97 to 122 for a-z // & 65 to 90 for A-Z // then display the results // scan holmes to start char ch; int frequency=0; do { frequency = 0; printf("Enter a character to find the frequency: "); scanf(" %c", &ch); if (ch == 10 || ch == 13 || ch == 33) continue; // lf, cr, ! //for (int j=65; j<91; j++) // small characters //{ //ch = (char) j; for(i = 0; holmes[i] != '\0'; i++) { if(ch == holmes[i]) ++frequency; } //} printf("\tFrequency of %c = %d\n", ch, frequency); ch = 'a'; } while (ch != 33); // break on exclamation point /* long test = 0; // use a largish number for (i=900000; i<1000002; i++) printf("%ld\n",i); */ //////////////////////////////////////////////////////////////////////////////// free(holmes); // free memory taken by Conan Doyle free(tom); // free memory taken by Sam Clemens free(raven); // free up the space taken by Poe free(huck); // free up memory taken by Twain return(0); // required by OS but not stictly enforced. } // end of main()