c++ - Print duplicates of strings -



c++ - Print duplicates of strings -

i working code find amount of duplicated words imported file. input stream represents file containing series of lines. function should examine each line looking consecutive occurrences of same token on same line , print each duplicated token along how many times appears consecutively. non-repeated tokens not printed.

here have:

#include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; int main() { ifstream in("file.txt"); if (! in) { cerr << "could not open file.txt."; homecoming exit_failure; } string str; int count = 0; int len=str.length(); while(getline(in,str)){ for(int = 0; < len; i++){ if(str.at(i) == str.at(i+1)){ count++; } else if(str.at(i) != str.at(i+1)){ i++; } } cout << str << "*" << count << endl; } }

the .txt contains:

hello how how you you i jack's jack's smirking smirking smirking smirking smirking revenge bow wow wow yippee yippee yo yippee yippee yay yay yay 1 fish 2 fish reddish fish bluish fish it's muppet show, wakka wakka wakka

the output should be:

how*2 you*4 i*3 jack's*2 smirking*5 wow*2 yippee*2 yippee*2 yay*3 wakka*3

#include <iostream> #include <string> #include <fstream> #include <cstdlib> using namespace std; int main() { ifstream in("file.txt"); if(!in){ cerr << "could not open file.txt."; homecoming exit_failure; } string str; string str2; string n; string tab[100]; string tab3[100]; unsigned int tab2[100]; unsigned int tab4[100]; unsigned int = 0; unsigned int k = 0; unsigned int l = 0; unsigned int tablenght; unsigned int tablenght2; k = 0; //it reads every line of text in file str2 while(getline(in,str2)){ //it add together every line of text str2 str whole file text str += str2; str += ' '; //you need add together character mark new line str += "0 "; } for(i = 0; < str.length(); i++){ /*you check every single character in string str if char not space writes string table tab, if char space adds 1 index write downwards next word in next index of table tab*/ if(str[i] != ' '){ tab[k] += str[i]; }else{ k++; //that 2 spaces if(str[i+1] == ' '){ k--; } } } //k+1 how many words , indexes wrote table tab tablenght = k+1; l = 0; k = 0; for(i = 0; < tablenght; i++){ //you need reset number of repeats k 0 if go line if(tab[i] == "0"){ k = 0; } //there number k how many times word repeats if(tab[i] == tab[i+1]){ k++; //you need reset k if tab current not equal tab next }else{ k = 0; } //there store k values integer table tab2 tab2[l] = k+1; l++; } l = 0; /*there need check if current string of table tab equal next string in table tab , if need set next string tab3[l] if dont you*4 you*4 you*4 you*4 instead of you*4*/ for(i = 0; < tablenght-1; i++){ if(tab[i] == tab[i+1]){ tab3[l] = tab[i+1]; tab4[l] = tab2[i]; }else{ l++; } if(tab[i+1] == "0"){ tab3[l] = tab[i+1]; } k++; } tablenght2 = l; //there cout both tables for(i = 0; < tablenght2; i++){ /*you need check if number bigger 1 because need cout words repeats more 1 time need check table tab3 doesnt contain string 0 added check later if needs go in line , need check tab3 index not empty*/ if(tab4[i] > 1 && tab3[i] != "0" && !tab3[i].empty()){ cout << tab3[i] << "*" << tab4[i] << " "; } /*thats 0 wrote table in begining , can write new line*/ if(tab3[i] == "0"){ cout << endl; } } homecoming 0; }

c++ duplicates

Comments

Popular posts from this blog

javascript - I need to update the text of a paragraph by inline edit -

javascript - THREE.js reposition vertices for RingGeometry -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -