Skip to main content

Find the number of repeated words in a string

Here given code implementation process.

// Include header file
#include <iostream>
#include <string.h>
#include <unordered_map>

using namespace std;
/*
    C++ program
    Find the number of repeated words in a string
*/
class Counting
{
	public: bool isValidWord(string word)
	{
		if ( word.compare("\t") == 0 
            || word.compare("\n") == 0 
            || word.compare(" ") == 0)
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	void countRepeatedwords(char *text)
	{
		int result = 0;
		// Collect all words of string
		char *words = strtok(text, " ");
		// Using of this to collect words and display status.
		unordered_map < string, bool > record;
		// Execute loop to check all words
		while (words != NULL)
		{
			if (this->isValidWord(words))
			{
				if (record.find(words) != record.end())
				{
					if (record[words])
					{
						cout << "  " << words;
						record[words] = false;
						result++;
					}
				}
				else
				{
					record[words] = true;
				}
			}
			words = strtok(NULL, " ");
		}
		// Display number of repeated words
		cout << "\n Number of repeated words is : " << result << endl;
	}
};
int main()
{
	Counting *task = new Counting();
	char text[] = "I Cream Apply Mango Ice Cream I Ice  Ice";
	task->countRepeatedwords(text);
	return 0;
}

input

  Cream  I  Ice
 Number of repeated words is : 3
import java.util.HashMap;
/*
    Java program
    Find the number of repeated words in a string
*/
public class Counting
{
	public boolean isValidWord(String word)
	{
		if (word.equals("\t") || word.equals("\n") || word.equals(" "))
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	public void countRepeatedwords(String text)
	{
		int result = 0;
		// Collect all words of string
		String[] words = text.split(" ");
		// Using of this to collect words and display status.
		HashMap < String, Boolean > record = new HashMap < String, Boolean > ();
		int n = words.length;
		// Execute loop to check all words
		for (int i = 0; i < n; i++)
		{
			if (isValidWord(words[i]))
			{
				if (record.containsKey(words[i]))
				{
					if (record.get(words[i]))
					{
						System.out.print("  " + words[i]);
						record.put(words[i], false);
						result++;
					}
				}
				else
				{
					record.put(words[i], true);
				}
			}
		}
		// Display number of repeated words
		System.out.println("\n Number of repeated words is : " + result);
	}
	public static void main(String[] args)
	{
		Counting task = new Counting();
		String text = "I Cream Apply Mango Ice Cream I Ice  Ice";
		task.countRepeatedwords(text);
	}
}

input

  Cream  I  Ice
 Number of repeated words is : 3
package main
import "strings"
import "fmt"
/*
    Go program
    Find the number of repeated words in a string
*/

func  isValidWord(word string) bool {
	if word == "\t" || word == "\n" || word == " "  {
		return false
	}
	// When word is not new line tabs and space characters
	// Note that not include all escape sequence
	return true
}
func  countRepeatedwords(text string) {
	var result int = 0
	// Collect all words of string
	var words =  strings.Split(text," ")
	// Using of this to collect words and display status.
	var record = make(map[string] bool)
	var n int = len(words)
	// Execute loop to check all words
	for i := 0 ; i < n ; i++ {
		if isValidWord(words[i]) {
			if _, found := record[words[i]] ; found {
				if record[words[i]] {
					fmt.Print("  ", words[i])
					record[words[i]] = false
					result++
				}
			} else {
				record[words[i]] = true
			}
		}
	}
	// Display number of repeated words
	fmt.Println("\n Number of repeated words is : ", result)
}
func main() {
	
	var text string = "I Cream Apply Mango Ice Cream I Ice  Ice"
	countRepeatedwords(text)
}

input

  Cream  I  Ice
 Number of repeated words is : 3
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program
    Find the number of repeated words in a string
*/
public class Counting
{
	public Boolean isValidWord(String word)
	{
		if (word.Equals("\t") || word.Equals("\n") || word.Equals(" "))
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	public void countRepeatedwords(String text)
	{
		int result = 0;
		// Collect all words of string
		String[] words = text.Split(" ");
		// Using of this to collect words and display status.
		Dictionary < string, bool > record = 
          		new Dictionary < string, bool > ();
		int n = words.Length;
		// Execute loop to check all words
		for (int i = 0; i < n; i++)
		{
			if (this.isValidWord(words[i]))
			{
				if (record.ContainsKey(words[i]))
				{
					if (record[words[i]])
					{
						Console.Write("  " + words[i]);
						record[words[i]] = false;
						result++;
					}
				}
				else
				{
					record.Add(words[i], true);
				}
			}
		}
		// Display number of repeated words
		Console.WriteLine("\n Number of repeated words is : " + result);
	}
	public static void Main(String[] args)
	{
		Counting task = new Counting();
		String text = "I Cream Apply Mango Ice Cream I Ice  Ice";
		task.countRepeatedwords(text);
	}
}

input

  Cream  I  Ice
 Number of repeated words is : 3
<?php
/*
    Php program
    Find the number of repeated words in a string
*/
class Counting
{
	public	function isValidWord($word)
	{
		if (strcmp($word, "\t") == 0 || strcmp($word, "\n")  == 0 || strcmp($word, " ") == 0)
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	public	function countRepeatedwords($text)
	{
		$result = 0;
		// Collect all words of string
		$words = explode(" ",$text);
		// Using of this to collect words and display status.
		$record = array();
		$n = count($words);
		// Execute loop to check all words
		for ($i = 0; $i < $n; $i++)
		{
			if ($this->isValidWord($words[$i]))
			{
				if (array_key_exists($words[$i], $record))
				{
					if ($record[$words[$i]])
					{
						echo("  ".$words[$i]);
						$record[$words[$i]] = false;
						$result++;
					}
				}
				else
				{
					$record[$words[$i]] = true;
				}
			}
		}
		// Display number of repeated words
		echo("\n Number of repeated words is : ".$result.
			"\n");
	}
}

function main()
{
	$task = new Counting();
	$text = "I Cream Apply Mango Ice Cream I Ice  Ice";
	$task->countRepeatedwords($text);
}
main();

input

  Cream  I  Ice
 Number of repeated words is : 3
/*
    Node JS program
    Find the number of repeated words in a string
*/
class Counting
{
	isValidWord(word)
	{
		if (word.localeCompare("\t") == 0 
            || word.localeCompare("\n")  == 0 
            || word.localeCompare(" ") == 0 )
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	countRepeatedwords(text)
	{
		var result = 0;
		// Collect all words of string
		var words = text.split(" ");
		// Using of this to collect words and display status.
		var record = new Map();
		var n = words.length;
		// Execute loop to check all words
		for (var i = 0; i < n; i++)
		{
			if (this.isValidWord(words[i]))
			{
				if (record.has(words[i]))
				{
					if (record.get(words[i]))
					{
						process.stdout.write("  " + words[i]);
						record.set(words[i], false);
						result++;
					}
				}
				else
				{
					record.set(words[i], true);
				}
			}
		}
		// Display number of repeated words
		console.log("\n Number of repeated words is : " + result);
	}
}

function main()
{
	var task = new Counting();
	var text = "I Cream Apply Mango Ice Cream I Ice  Ice";
	task.countRepeatedwords(text);
}
main();

input

  Cream  I  Ice
 Number of repeated words is : 3
#    Python 3 program
#    Find the number of repeated words in a string
class Counting :
	def isValidWord(self, word) :
		if (word == "\t"
			or word == "\n"
			or word == " ") :
			return False
		
		#  When word is not new line tabs and space characters
		#  Note that not include all escape sequence
		return True
	
	def countRepeatedwords(self, text) :
		result = 0
		#  Collect all words of string
		words = text.split(" ")
		#  Using of this to collect words and display status.
		record = dict()
		n = len(words)
		i = 0
		#  Execute loop to check all words
		while (i < n) :
			if (self.isValidWord(words[i])) :
				if ((words[i] in record.keys())) :
					if (record.get(words[i])) :
						print("  ", words[i], end = "")
						record[words[i]] = False
						result += 1
					
				else :
					record[words[i]] = True
				
			
			i += 1
		
		#  Display number of repeated words
		print("\n Number of repeated words is : ", result)
	

def main() :
	task = Counting()
	text = "I Cream Apply Mango Ice Cream I Ice  Ice"
	task.countRepeatedwords(text)

if __name__ == "__main__": main()

input

   Cream   I   Ice
 Number of repeated words is :  3
#    Ruby program
#    Find the number of repeated words in a string
class Counting 
	def isValidWord(word) 
		if (word === "\t" || word === "\n" || word === " ") 
			return false
		end

		#  When word is not new line tabs and space characters
		#  Note that not include all escape sequence
		return true
	end

	def countRepeatedwords(text) 
		result = 0
		#  Collect all words of string
		words = text.split(" ")
		#  Using of this to collect words and display status.
		record = Hash.new()
		n = words.length
		i = 0
		#  Execute loop to check all words
		while (i < n) 
			if (self.isValidWord(words[i])) 
				if (record.key?(words[i])) 
					if (record[words[i]]) 
						print("  ", words[i])
						record[words[i]] = false
						result += 1
					end

				else
 
					record[words[i]] = true
				end

			end

			i += 1
		end

		#  Display number of repeated words
		print("\n Number of repeated words is : ", result, "\n")
	end

end

def main() 
	task = Counting.new()
	text = "I Cream Apply Mango Ice Cream I Ice  Ice"
	task.countRepeatedwords(text)
end

main()

input

  Cream  I  Ice
 Number of repeated words is : 3
import scala.collection.mutable._;
/*
    Scala program
    Find the number of repeated words in a string
*/
class Counting()
{
	def isValidWord(word: String): Boolean = {
		if (word.equals("\t") || word.equals("\n") || word.equals(" "))
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	def countRepeatedwords(text: String): Unit = {
		var result: Int = 0;
		// Collect all words of string
		var words: Array[String] = text.split(" ");
		// Using of this to collect words and display status.
		var record: HashMap[String, Boolean] = new HashMap[String, Boolean]();
		var n: Int = words.length;
		var i: Int = 0;
		// Execute loop to check all words
		while (i < n)
		{
			if (isValidWord(words(i)))
			{
				if (record.contains(words(i)))
				{
					if (record.get(words(i)).get)
					{
						print("  " + words(i));
						record.addOne(words(i), false);
						result += 1;
					}
				}
				else
				{
					record.addOne(words(i), true);
				}
			}
			i += 1;
		}
		// Display number of repeated words
		println("\n Number of repeated words is : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Counting = new Counting();
		var text: String = "I Cream Apply Mango Ice Cream I Ice  Ice";
		task.countRepeatedwords(text);
	}
}

input

  Cream  I  Ice
 Number of repeated words is : 3
import Foundation;
/*
    Swift 4 program
    Find the number of repeated words in a string
*/
class Counting
{
	func isValidWord(_ word: String) -> Bool
	{
		if (word == "\t" || word == "\n" || word == " ")
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	func countRepeatedwords(_ text: String)
	{
		var result: Int = 0;
		// Collect all words of string
		let words: [String] = text.split{$0 == " "}.map(String.init);
		// Using of this to collect words and display status.
		var record = [String : Bool]();
		let n: Int = words.count;
		var i: Int = 0;
		// Execute loop to check all words
		while (i < n)
		{
			if (self.isValidWord(words[i]))
			{
				if (record.keys.contains(words[i]))
				{
					if (record[words[i]]!)
					{
						print("  ", words[i], terminator: "");
						record[words[i]] = false;
						result += 1;
					}
				}
				else
				{
					record[words[i]] = true;
				}
			}
			i += 1;
		}
		// Display number of repeated words
		print("\n Number of repeated words is : ", result);
	}
}
func main()
{
	let task: Counting = Counting();
	let text: String = "I Cream Apply Mango Ice Cream I Ice  Ice";
	task.countRepeatedwords(text);
}
main();

input

   Cream   I   Ice
 Number of repeated words is :  3
/*
    Kotlin program
    Find the number of repeated words in a string
*/
class Counting
{
	fun isValidWord(word: String): Boolean
	{
		if (word.equals("\t") || word.equals("\n") || word.equals(" "))
		{
			return false;
		}
		// When word is not new line tabs and space characters
		// Note that not include all escape sequence
		return true;
	}
	fun countRepeatedwords(text: String): Unit
	{
		var result: Int = 0;
		// Collect all words of string
		val words = text.split(" ");
		// Using of this to collect words and display status.
		val record: HashMap < String, Boolean > = HashMap < String, Boolean > ();
		val n: Int = words.count();
		var i: Int = 0;
		// Execute loop to check all words
		while (i < n)
		{
			if (this.isValidWord(words[i]))
			{
				if (record.containsKey(words[i]))
				{
					if (record.getValue(words[i]))
					{
						print("  " + words[i]);
						record.put(words[i], false);
						result += 1;
					}
				}
				else
				{
					record.put(words[i], true);
				}
			}
			i += 1;
		}
		// Display number of repeated words
		println("\n Number of repeated words is : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Counting = Counting();
	val text: String = "I Cream Apply Mango Ice Cream I Ice  Ice";
	task.countRepeatedwords(text);
}

input

  Cream  I  Ice
 Number of repeated words is : 3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment