Advertisement
purxiz

Much Better Find String Code

Nov 24th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7. int main () {
  8.   ifstream f;
  9.   f.open ("nospace2.txt");
  10.   const int n = 41;
  11.   char b[n] = "";
  12.   struct data{
  13.     char digits[n];
  14.     int repeats;
  15.   };
  16.   vector<data> combo;
  17.   int count = 0;
  18.  
  19.   while(f.read(b, sizeof(b) - 1)){
  20.     int found = 0;
  21.     //printf("%s\n", b);
  22.     for(int j = 0; j < combo.size(); j++){
  23.       //printf("comparing b = %s and digits = %s ", b, combo[j].digits);
  24.       if (strcmp(b, combo[j].digits) == 0) {
  25.           combo[j].repeats++;
  26.           found = 1;
  27.           //printf("They match!");
  28.       }
  29.       //printf("\n");
  30.     }
  31.     if(found == 0){
  32.       combo.push_back(data());
  33.       strcpy(combo[count].digits, b);
  34.       combo[count++].repeats = 1;
  35.       //printf("no match was found, setting combo[%d].repeats = %d and digits = %s\n", count - 1, combo[count - 1].repeats, combo[count - 1].digits); //ignore
  36.     }
  37.     f.seekg((-n+9), f.cur);
  38.   }
  39.  
  40.   vector<int> max;
  41.   vector<int> pos;
  42.  
  43.   for (int k = 0; k < combo.size(); k++) {
  44.     max.push_back(-5000000);
  45.     pos.push_back(0);
  46.     for(int i = 0; i < combo.size(); i++){
  47.       //printf("%d\n", combo[i][0]); //ignore
  48.       if(combo[i].repeats > max[k]){
  49.         max[k] = combo[i].repeats;
  50.         pos[k] = i;
  51.       }
  52.     }
  53.     printf("%s occured %d times\n", combo[pos[k]].digits, max[k]);
  54.     combo[pos[k]].repeats = -1;
  55.   }
  56.   f.close();
  57.   return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement