C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to find the duplicate words in a stringExplanationIn this program, we need to find out the duplicate words present in the string and display those words. big black bug bit a big black dog on his big black nose To find the duplicate words from the string, we first split the string into words. We count the occurrence of each word in the string. If count is greater than 1, it implies that a word has duplicate in the string. In above example, the words highlighted in green are duplicate words. Algorithm
SolutionPythonstring = "big black bug bit a big black dog on his big black nose"; #Converts the string into lowercase string = string.lower(); #Split the string into words using built-in function words = string.split(" "); print("Duplicate words in a given string : "); for i in range(0, len(words)): count = 1; for j in range(i+1, len(words)): if(words[i] == (words[j])): count = count + 1; #Set words[j] to 0 to avoid printing visited word words[j] = "0"; #Displays the duplicate word if count is greater than 1 if(count > 1 and words[i] != "0"): print(words[i]); Output: Duplicate words in a given string : big black C#include <stdio.h> #include <string.h> int main() { char string[] = "big black bug bit a big black dog on his big black nose"; char words[100][100]; int i = 0, j = 0, k, length, count; //Split the string into words such that each row of array words represents a word for(k=0; string[k]!='\0'; k++){ //Here, i represents row and j represents column of two-dimensional array words if(string[k] != ' ' && string[k] != '\0'){ //Converts the string into lowercase and add it to array words words[i][j++] = tolower(string[k]); } else{ words[i][j] = '\0'; //Increment row count to store new word i++; //Set column count to 0 j = 0; } } //Store row count in variable length length = i+1; printf("Duplicate words in the given string: \n"); for(i = 0; i < length; i++){ count = 1; for(j = i+1; j < length; j++){ if(strcmp(words[i], words[j]) == 0 && (strcmp(words[j],"0") != 0)){ count++; //Set words[j] to 0 to avoid printing visited word strcpy(words[j],"0"); } } //Displays the duplicate word if count is greater than 1 if(count > 1 ) printf("%s\n", words[i]); } return 0; } Output: Duplicate words in a given string : big black JAVApublic class DuplicateWord { public static void main(String[] args) { String string = "Big black bug bit a big black dog on his big black nose"; int count; //Converts the string into lowercase string = string.toLowerCase(); //Split the string into words using built-in function String words[] = string.split(" "); System.out.println("Duplicate words in a given string : "); for(int i = 0; i < words.length; i++) { count = 1; for(int j = i+1; j < words.length; j++) { if(words[i].equals(words[j])) { count++; //Set words[j] to 0 to avoid printing visited word words[j] = "0"; } } //Displays the duplicate word if count is greater than 1 if(count > 1 && words[i] != "0") System.out.println(words[i]); } } } Output: Duplicate words in a given string : big black C#using System; public class DuplicateWord { public static void Main() { String string1 = "Big black bug bit a big black dog on his big black nose"; int count; //Converts the string into lowercase string1 = string1.ToLower(); //Split the string into words using built-in function String[] words = string1.Split(' '); Console.WriteLine("Duplicate words in a given string : "); for(int i = 0; i < words.Length; i++) { count = 1; for(int j = i+1; j < words.Length; j++) { if(words[i].Equals(words[j])) { count++; //Set words[j] to 0 to avoid printing visited word words[j] = "0"; } } //Displays the duplicate word if count is greater than 1 if(count > 1 && words[i] != "0") Console.WriteLine(words[i]); } } } Output: Duplicate words in a given string : big Black PHP<!DOCTYPE html> <html> <body> <?php $string = "Big black bug bit a big black dog on his big black nose"; $count; //Converts the string into lowercase $string = strtolower($string); //Split the string into words using built-in function $words = explode(" ", $string); print("Duplicate words in a given string : <br>"); for($i = 0; $i < count($words); $i++) { $count = 1; for($j = $i+1; $j < count($words); $j++) { if($words[$i] == $words[$j]) { $count++; //Set words[j] to 0 to avoid printing visited word $words[$j] = "0"; } } //Displays the duplicate word if count is greater than 1 if($count > 1 && $words[$i] != "0"){ print($words[$i]); print("<br>"); } } ?> </body> </html> Output: Duplicate words in a given string : big black
Next Topic#
|