Home > OS >  How do you change repeating letters in a sentence, depending on the amount of times they repeat them
How do you change repeating letters in a sentence, depending on the amount of times they repeat them

Time:01-11

Long story short I'm doing my homework assignment and what I have to do is basically to decrypt a huge text block. Everything is scrambled/"encrypted" and you have to decrypt it. How does it work? Some letters repeat more than others and you can decrypt the text with finding the most repeating letters/letter combinations. How I decided to do it, is that I read "encrypted.txt" and I count how many letters are repeating. Then I sort them in a descending order so that it's easier to know which letter is repeated the most and I can find which in the English alphabet it is. Online there is a letter frequency table which shows which letters repeat the most and etc. And everything must be printed/outputed into "decrypted.txt".

I'll give you a similar example, in "encrypted.txt" there is this sentence AAAAAA BB RRRR L which when decrypted should look like this: EEEEEE AA TTTT O. The most repeating letter is A (which repeats 6 times) then R (which repeats 4 times) then B (which repeats 2 times) and L (which repeats only 1 time). So the letter frequency table says that E has a frequency of 12.02%, T has a frequency of 9.1%, A has a frequency of 8.12% and O has a frequency of 7.68%. So taking everything what we have, A must be replaced by E, R by T, B by A and L by O.

The problem that I can't figure out how to solve, is that when I find, count and sort all the letters I don't know how to change them to the corresponding ones. I need to pass 20 tests and the encrypted text changes each time, so the code has to decrypt at least 90% of the text and in short it has to be versatile, that way it counts the letters and finds out which letter is which and changes it.

I read the whole text file and count how many times each letter repeats itself with one of those Ax, Bx, Lx, Rx. Then I transfer the values stored in Ax, Bx, Lx, Rx to my Array[3] so I can sort them in a descending order. Once that's done I store the sorted values in these most1, most2, most3, most4 and I'm trying to use them to help me find a corresponding letter. But I just can't figure out how to do that. How I envisioned my code to work:

  1. I read the text file;
  2. I count how many letters repeat and which ones exactly;
  3. I put them into an array and sort them in a descending order;
  4. I use most1, most2, most3, most4 to mark all of the values;
  5. Once that's done, I read the file again and read a letter and see which letter it is, how many times it appeared (by using most1/most2/most3/most4) and then change it to the needed letter (A must be replaced by E, R by T, B by A and L by O);

It would be much easier to solve, if the encrypted text would stay the same. But in each test it changes. Apologies for the messy code and my lack of knowledge.

My code:


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
  string str;
  int most1,most2,most3,most4;///Thinking of using these to find out which letter it is 
  int n=4;///How many letters there are(for sorting)
  int Array[3];///An array where I put the count of all letters and then sort it
  int Ax=0, Bx=0, Lx=0, Rx=0;///With these I store the sum of each letter (how many times they repeat themselves)

  ifstream read("encrypted.txt");
  ofstream write("decrypted.txt");
  while (getline(read,str)) {
    for(char &ch:str) {
        if(ch=='A'){
          Ax  ;///If a specific letter is found, I count it
          Array[0]=Ax;///Transferring the count of letters found to the array
        }
        if(ch=='B'){
          Bx  ;///If a specific letter is found, I count it
          Array[1]=Bx;///Transferring the count of letters found to the array
        }
        if(ch=='L'){
          Lx  ;///If a specific letter is found, I count it
          Array[2]=Lx;///Transferring the count of letters found to the array
        }
        if(ch=='R'){
          Rx  ;///If a specific letter is found, I count it
          Array[3]=Rx;///Transferring the count of letters found to the array
        }
        }
      }

  sort(Array, Array n, greater<int>());///Sorting the Array in a descending order

most1=Array[0];/// value in here is 6
most2=Array[1];/// value in here is 4
most3=Array[2];/// value in here is 2
most4=Array[3];/// value in here is 1

 ///Here I read the file again in order to replace the letters with the corresponding ones
read.close();
read.clear();
read.seekg(0, read.beg);
read.open("encrypted.txt");

  while (getline(read,str)) {
    for(char &ch:str) {
      if(ch=='A'){///How do you read a letter and check how many times it repeats itself and then determine which letter it has to be depending on the amount of times it repeated itself.
       ch='E';
      }
      if(ch=='R'){
       ch='T';
      }
      if(ch=='B'){
       ch='A';
      }
      if(ch=='A'){
       ch='L';
      }
    }
    write<<str<<endl;
  }

  read.close();
  write.close();
  return 0;
}

CodePudding user response:

the logic to count letters is simple

std::map<char, int> counts;
for(char &ch:str) {
    ch = tolower(ch);
    counts[ch]  ;
}

do that first and simply then print out

a:1
b:3
.....

get this bit going. Next you need to sort by count. I would walk the map and load into an array of struct

 struct letter_freq{
     char letter;
     int count;
 }

then sort that array on count

CodePudding user response:

Hi so actually you can do it in a simple way. Each letter has its own ascii so create an integer array of size 177 which is total number of entities. So pass char array through a loop and for each character add 1 on ascii position of that character. At last you have the ascii value of each element as its index and its count as its value. Now in order to sort all this create pairs where you store ascii value (index) and the count (value) for each element ignoring elements with 0 value. And simply replace that with your decryption data after sorting.

  •  Tags:  
  • c
  • Related