Maximum repeating word per line

I tried to make a very common problem with the interview "Search for the maximum repeated word in a string" and could not find many resources on the network for implementing c / C ++. Therefore, I myself encoded it. I tried to make most of the code from scratch for a better understanding. Could you please review my code and provide comments on my algorithm. Some people suggested using hashtables to store count, but I do not use hashtables here.

#include<stdafx.h>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
string word[10];

//splitting string into words
int parsestr(string str)
{   
    int index = 0;
    int i = 0;
    int maxlength = str.length();
    int wordcnt = 0;
    while(i < maxlength)
    {
        if(str[i]!= ' ')
        {
            word[index] = word[index]+str[i];
        }
        else
        {
            index++;//new word
            wordcnt = index;
        }
    i++;
    }
    return wordcnt;
}

//find the max word count out of the array and return the word corresponding to that index.
string maxrepeatedWord(int wordcntArr[],int count)
{
int max = 0;
int index = 0;
    for(int i=0;i<=count;i++)
    {
        if(wordcntArr[i] > max)
        {
        max = wordcntArr[i];
        index = i;
        }
    }

    return word[index];
}
void countwords(int count)
{
    int wordcnt = 0;
    int wordcntArr[10];
    string maxrepeatedword;
    for(int i=0;i<=count ;i++)
    {
        for(int j=0;j<=count;j++)
        {
            if(word[i]==word[j])
            {
            wordcnt++;
            //word[j] = "";
            }
            else
            {}
        }
        cout<<" word "<< word[i] <<" occurs "<< wordcnt <<" times "<<endl;
        wordcntArr[i] = wordcnt;
        wordcnt = 0;
    }

    maxrepeatedword = maxrepeatedWord(wordcntArr,count);
    cout<< " Max Repeated Word is " << maxrepeatedword;
}

int main()
{
string str = "I am am am good good";
int wordcount = 0;
wordcount = parsestr(str);
countwords(wordcount);
}
+3
source share
3 answers

Just for comparison, the most obvious way to do this is C ++:

#include <map>
#include <string>
#include <iostream>
#include <sstream>

int main()
{
    std::istringstream input("I am am am good good");
    std::map<std::string, int> count;
    std::string word;
    decltype(count)::const_iterator most_common;
    while (input >> word)
    {
        auto iterator = count.emplace(word, 0).first;
        ++iterator->second;
        if (count.size() == 1 ||
            iterator->second > most_common->second)
            most_common = iterator;
    }
    std::cout << '\"' << most_common->first << "' repeated "
         << most_common->second << " times\n";
}

See here here .

Notes:

  • map::emplace pair<iterator,bool>, , map, . , emplace(...).first.

  • , , , . , most_common, , .

, , :

  • word - - , , , , , . , countwords(wordcount) , : int wordcount.
  • : 10 , . ++ .
  • , std::string::operator+=(char) char ala my_string += my_char;

, , , , .

+5
import java.util.*;

public class StringWordDuplicates {

    static void duplicate(String inputString){

        HashMap<String, Integer> wordCount = new HashMap<String,Integer>();
        String[] words = inputString.split(" ");

        for(String word : words){
            if(wordCount.containsKey(word)){
                wordCount.put(word, wordCount.get(word)+1);             
            }
            else{
                wordCount.put(word, 1);
            }
        }
        //Extracting of all keys of word count
        Set<String> wordsInString = wordCount.keySet();

        for(String word : wordsInString){
            if(wordCount.get(word)>1){
                System.out.println(word+":"+wordCount.get(word));
            }
        }

    }
    public static void main(String args[]){
        duplicate("I am Java Programmer and IT Server Programmer with Java as Best Java lover");

    }
}
+1

Code snippet:

void mostRepeat(string words[], int n)
{
    int hash[n]={0};

    for(int j=0; j<n; j++)
    {
      for(int i=0; i<n; i++)
      {
        if(words[j]==words[i])  hash[j]++;
      }
    }

    int maxi = hash[0];
    int index = 0;

    for(int i=0; i<n; i++)
    {
       if(maxi<hash[i])
       {
          maxi=hash[i];
          index = i;
       }
    }
    cout<<words[index]<<endl;
}

Full program: Link

0
source

All Articles