title: Most common words in txt File
body: <p>I've made a program that outputs you the most common word in txt file.
  And does anybody know how to optimize it that it would work for bigger
  files and faster.</p>
  
  <p>The output image: <a href="http://i.stack.imgur.com/fVBh0.png" rel="nofollow">http://i.stack.imgur.com/fVBh0.png</a></p>
  
  <p>Here is the code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  int main()
  {
      ifstream in("file.txt");
  
      if(!in){
          cerr &lt;&lt; "Could not open file.txt.";
          return EXIT_FAILURE;
      }
  
      string str, str2, strn, tab[10000], tab2[10000];
      int i, k, j, n, l, tabl;
      char c = 179;
      vector&lt;int&gt; tabs;
      vector&lt;string&gt; stringi;
  
      while(getline(in, str2)){
          str += str2;
          str += ' ';
      }
      k = 0;
      for(i = 0; i &lt; str.length(); i++){
          if(str[i] != ' ' &amp;&amp; str[i] != '.' &amp;&amp; str[i] != '\t' &amp;&amp; str[i] != ','
             &amp;&amp; str[i] != ';' &amp;&amp; str[i] != ':' &amp;&amp; str[i] != '}' &amp;&amp; str[i] != '{'){
              tab[k] += tolower(str[i]);
          }else{
              k++;
          }
          if(str[i] == '.' || str[i] == '\t' || str[i] == ',' || str[i] == ';'
          || str[i] == ':' || str[i] == '}' || str[i] == '{') {
              k--;
          }
      }
      tabl = k;
  
      k = 0;
      for(i = 0; i &lt; tabl; i++){
          for(j = 0; j &lt; tabl; j++){
              if(tab[i] == tab[j]){
                  k++;
              }
          }
          tabs.push_back(k);
          k = 0;
      }
      for(i = 0; i &lt; tabl; i++){
          for(j = 0; j &lt; tabl-1; j++){
              if(tab[j] &lt; tab[j+1]){
                  n = tabs.at(j);
                  tabs.at(j) = tabs.at(j+1);
                  tabs.at(j+1) = n;
                  strn = tab[j];
                  tab[j] = tab[j+1];
                  tab[j+1] = strn;
              }
          }
      }
      for(i = 0; i &lt; tabl; i++){
          for(j = 0; j &lt; tabl-1; j++){
              if(tabs.at(j) &lt; tabs.at(j+1)){
                  n = tabs.at(j);
                  tabs.at(j) = tabs.at(j+1);
                  tabs.at(j+1) = n;
                  strn = tab[j];
                  tab[j] = tab[j+1];
                  tab[j+1] = strn;
              }
          }
      }
      tab2[0] = tab[0];
      for(i = 0; i &lt; tabl; i++){
          if(tab[i] != tab[i+1]){
              tab2[i] = tab[i+1];
          }
      }
      k = 1;
      l++;
      for(i = 0; i &lt; tabl; i++){
          if(!tab2[i].empty()){
              l++;
          }
      }
      cout &lt;&lt; "------------------------------------" &lt;&lt; endl;
      cout &lt;&lt; "|---&gt;TABLE OF MOST COMMON WORDS&lt;---|" &lt;&lt; endl;
      cout &lt;&lt; "------------------------------------" &lt;&lt; endl;
      for(i = 0; i &lt; tabl; i++){
          if(!tab2[i].empty() &amp;&amp; k &lt;= 20 ){
              cout &lt;&lt; c &lt;&lt; k++ &lt;&lt; "." &lt;&lt; '\t' &lt;&lt; c &lt;&lt; tab2[i] &lt;&lt; '\t' &lt;&lt; c &lt;&lt; "*" &lt;&lt;
              tabs.at(i+1)
              &lt;&lt; '\t'  &lt;&lt; c &lt;&lt; roundf(((float)tabs.at(i+1)*100/l)*100)/100 &lt;&lt; "%" &lt;&lt;
              endl;
          }
      }
      cout &lt;&lt; "------------------------------------" &lt;&lt; endl ;
      cout &lt;&lt; "|-----&gt;Dif. strings: " &lt;&lt; '\t' &lt;&lt; l &lt;&lt; "&lt;-------|" &lt;&lt; endl ;
      cout &lt;&lt; "------------------------------------" &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>