打印重复的字符串

时间:2014-10-11 22:40:10

标签: c++ duplicates

我正在编写一个代码,用于查找导入文件中重复单词的数量。 输入流表示包含一系列行的文件。功能应该 检查每一行在同一行上查找同一令牌的连续出现次数 打印每个重复的标记,连续出现多少次。不重复 代币不打印。

这就是我所拥有的:

#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.";
        return EXIT_FAILURE;
    }

    string str;

    int count = 0;
    int len=str.length();

    while(getline(in,str)){

        for(int i = 0; i < 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;
    }
}

.txt包含:

hello how how are you you you you
I I I am Jack's Jack's smirking smirking smirking smirking smirking revenge
 bow wow wow yippee yippee yo yippee yippee yay yay yay
one fish two fish red fish blue fish
It's the Muppet Show, wakka wakka wakka

输出应为:

how*2 you*4
I*3 Jack's*2 smirking*5
wow*2 yippee*2 yippee*2 yay*3

wakka*3

2 个答案:

答案 0 :(得分:0)

a = list('这是我的笔记本电脑')

b = list(set(a))

对于b中的i:

if i==' ':

    continue

c=a.count(i)

if c>1:

    print('{} is {}'.format(i,c))

答案 1 :(得分:-1)

#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.";
        return EXIT_FAILURE;
    }

    string str;
    string str2;
    string n;
    string tab[100];
    string tab3[100];
    unsigned int tab2[100];
    unsigned int tab4[100];
    unsigned int i = 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 every line of text str2 to str so you get whole file text
        str += str2;
        str += ' ';
        //you need to add a character to mark where was the new line
        str += "0 ";
    }

    for(i = 0; i < str.length(); i++){
            /*you check every single character in string str if that char is not
            space than it writes it to string table tab, if that char is space than it 
            adds one to your index so it will write down the next word in next  
            index of table tab*/
            if(str[i] != ' '){
                tab[k] += str[i];
            }else{
                k++;
                //that is for two spaces
                if(str[i+1] == ' '){
                    k--;
                }
            }
    }
    //k+1 is actually how many words and indexes you wrote to table tab
    tablenght = k+1;

    l = 0;
    k = 0;
    for(i = 0; i < tablenght; i++){
        //you need to reset the number of repeats k to zero if you go to another line
        if(tab[i] == "0"){
            k = 0;
        }
        //there you get the number k how many times does some word repeats itself
        if(tab[i] == tab[i+1]){
            k++;
        //you need to reset k if tab current is not equal to tab next
        }else{
            k = 0;
        }
        //there you store k values into integer table tab2
        tab2[l] = k+1;
        l++;
    }

    l = 0;
    /*there you need to check if current string of table tab is equal to next string
    in table tab and if it is you need to set next string to tab3[l] if you dont do
    that you get something like that you*4 you*4 you*4 you*4 instead of only you*4*/
    for(i = 0; i < 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 you cout both tables
    for(i = 0; i < tablenght2; i++){
        /*you need to check if number is bigger than 1 because it need to cout only
        the words that repeats itself for more than one time than you need to check
        that table tab3 doesnt contain string with zero that we previously added
        that we could check later if it needs to go in another line and we 
        need to check that tab3 index is not empty*/
        if(tab4[i] > 1 && tab3[i] != "0" && !tab3[i].empty()){
            cout << tab3[i] << "*" << tab4[i] << " ";
        }
        /*thats the zero that we wrote to table in the begining and that we can now
        write a new line*/
        if(tab3[i] == "0"){
            cout << endl;
        }
    }

    return 0;
}