Answers for "delete repeated words in string"

C
0

remove duplicate from string

// Java program to create a unique string
import java.util.*;
  
class IndexOf {
      
    // Function to make the string unique
    public static String unique(String s)
    {
        String str = new String();
        int len = s.length();
          
        // loop to traverse the string and
        // check for repeating chars using
        // IndexOf() method in Java
        for (int i = 0; i < len; i++) 
        {
            // character at i'th index of s
            char c = s.charAt(i);
              
            // if c is present in str, it returns
            // the index of c, else it returns -1
            if (str.indexOf(c) < 0)
            {
                // adding c to str if -1 is returned
                str += c;
            }
        }
          
        return str;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        // Input string with repeating chars
        String s = "geeksforgeeks";
          
        System.out.println(unique(s));
    }
}
Posted by: Guest on December-28-2021
0

delete repeated words in string

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main ()
{
	char str[100], word[100], twoD[10][30];
	int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, l = 0;
 
	printf ("Enter the string\n");
	gets (str);
 
	// let us convert the string into 2D array
	for (i = 0; str[i] != '\0'; i++)
	{
		if (str[i] == ' ')
		{
			twoD[k][j] = '\0';
			k ++;
			j = 0;
		}
		else
		{
			twoD[k][j] = str[i];
			j ++;
		}
	}
 
	twoD[k][j] = '\0';
 
	j = 0;
	for (i = 0; i < k; i++)
	{
		int present = 0;
		for (l = 1; l < k + 1; l++)
		{
			if (twoD[l][j] == '\0' || l == i)
			{
				continue;
			}
 
			if (strcmp (twoD[i], twoD[l]) == 0) {
				twoD[l][j] = '\0';
				present = present + 1;
			}
		}
		// if (present > 0)	     | uncomment this `if` block if you
		// {			     | want to remove all the occurrences 
		// 	twoD[i][j] = '\0';   | of the words including the word
		// }			     | itself.
	}
 
	j = 0;
 
	for (i = 0; i < k + 1; i++)
	{
		if (twoD[i][j] == '\0')
			continue;
		else
			printf ("%s ", twoD[i]);
	}
 
	printf ("\n");
 
	return 0;
}
Posted by: Guest on May-02-2022
0

Remove all duplicates words from a given sentence

from collections import Counter
 
def remov_duplicates(input):
 
    # split input string separated by space
    input = input.split(" ")
 
    # now create dictionary using counter method
    # which will have strings as key and their
    # frequencies as value
    UniqW = Counter(input)
 
    # joins two adjacent elements in iterable way
    s = " ".join(UniqW.keys())
    print (s)
 
# Driver program
if __name__ == "__main__":
    input = 'Python is great and Java is also great'
    remov_duplicates(input)
Posted by: Guest on February-23-2022

Code answers related to "delete repeated words in string"

Code answers related to "C"

Browse Popular Code Answers by Language