0

I've been working on this for awhile now, and I haven't been able to solve the issue. Essentially, if a user were to be given a ".txt" file in which the following was written, "tHe@doG#wENt&uP$tHE!hiLL!", the end result would need to look like this:

the
hed
edo
dog
ogw
gwe
wen
ent
ntu
...

I could go on but I'm sure it's self-explanatory. The code I've been using to try and accomplish this is shown below.

#include "functions.h"
#include "bigint/bigint.h"
#include <ctype.h>
#include <string>
#include <cstdio>
#include <stdio.h>


int main(int argc, char *argv[]) {

std::vector<std::string> v(17575);
std::string tri1;
std::string tri2;
std::string tri3;

for (int i = 1; i < argc; i++) {

    char* filename = argv[i];
    std::ifstream infile;

    infile.open(filename);

    // executes for discoverable files  
    if(! infile.fail()) {

        char ch;

        while (infile.get(ch)) {

            if (isspace(ch) || ispunct(ch) || isdigit(ch)) {
                continue;
            }

            else if (isupper(ch)) {
                ch = tolower(ch);
            }

            tri1 += ch;

            char ch2 = infile.peek();

            if (isspace(ch2) || ispunct(ch2) || isdigit(ch2)) {
                ch2 = infile.peek() + 1;
            }

            else if (isupper(ch2)) {
                ch2 = islower(ch2);
            }

            tri2 += ch2;

            char ch3 = infile.peek() + 1;

            if (isspace(ch3) || ispunct(ch3) || isdigit(ch3)) {
                ch3 = infile.peek() + 2;
            }

            else if (isupper(ch3)) {
                ch3 = islower(ch3);
            }

            tri3 += ch3;

            if (tri1.length() == 3) {
                std::cout << tri1 << std::endl;                 
                v.push_back(tri1);
                tri1 = "";
            }

            if (tri2.length() == 3) {
                std::cout << tri2 << std::endl;                 
                v.push_back(tri2);
                tri2 = "";
            }

            if (tri3.length() == 3) {
                std::cout << tri3 << std::endl;                 
                v.push_back(tri3);
                tri3 = "";
            }

        } //while


        infile.close();
    }

    // executes for non-discoverable files
    else {
        std::cerr << "Unable to open file: " << filename << '\n';
    }
}

return 0;

}

This has to do with Frequency Analysis of Trigrams (the vectors I'm going to be using later), and I feel like there is a way to shrink the amount of conditionals I have down, I just don't know how. Any help will be greatly appreciated!

  • 1
    So, does the code work? Your question is unclear. If your code has a problem, you need to detail the problem. If it works fine as is, you might be looking for Code Review SE. You seem to be ignoring numbers, which you should mention in your input – Tas Apr 22 '18 at 03:13
  • Is this a homework assignment, or a silly question from some silly online quiz site? Either way, the main portion of the correct solution should be a simple loop consisting of maybe 4-5 lines of code. No need for three `std::string`s. No need for a hairball of `if` statements. If your code ends up anything other than a single `char[4]` array, one loop, one `int` variable, and exactly two `if` statements, you're going to fail your homework assignment, or online quiz. – Sam Varshavchik Apr 22 '18 at 03:20
  • The code I have doesn't work, and I'm not really sure how I can fix it –  Apr 22 '18 at 03:26
  • Have you tried [discussing this subject matter with your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging)? What advice did your rubber duck give you? – Sam Varshavchik Apr 22 '18 at 03:38
  • I never knew there was a name for this process of debugging, but I like it. Unfortunately, I have only been working with C++ for a short time now, so my rubber duck initially thought the collection of if statements should work, not efficiently, but still work. –  Apr 22 '18 at 03:43
  • Try discussing the following approach with your rubber duck: Step 1: clear a `char[4]` array to all zeroes. Step 2: read the input file, one character at a time. Step 3: if the character is not a letter, continue. Step 4: shift the first three characters in the char array, i.e. char[0]=char[1], char[1]=char[2], and char[2]=char[3]. Put the tolower(new letter) into char[3]. Step 5: Increment the counter, and if it's at least 3, your next trigraph is in the char buffer. The End. Like I said, four or five lines of code, no more. – Sam Varshavchik Apr 22 '18 at 03:51

0 Answers0