Problem Statement:- Program to Remove duplicate words from a given string.
Example:-
Input: Given
String=C STRING PROGRAM PROGRAM STRING C
Output: PROGRAM STRING C
Input: Given
String=c++ string question question string c++
Output: c++ string question
Data requirement:-
Input Data:- str
Output Data:-sub_str
Output Data:-sub_str
Additional Data:- inn, p, in, out, len1
Program in C
Here is the source code of the C Program to Remove duplicate words from a given string.
Code:
#include<stdio.h>
#include<string.h>
main()
{
int in=0,out=0,p=0,len1;
char str[100]={0},sub_str[100][100]={0};
printf("Enter your String:");
gets(str);
//splitting Input String into sub string
while(str[p]!='\0')
{
out=0;
while(str[p]!=' ' && str[p]!='\0')
{
sub_str[in][out]=str[p];
p++;
out++;
}
sub_str[in][out]='\0';
in++;
if(str[p]!='\0')
{
p++;
}
}
printf("After removing duplicate words from a given String is:");
len1=in;
for(in=0;in<len1;in++)
{
for(out=in+1;out<len1;)
if(strcmp(sub_str[in],sub_str[out])==0)
{
for(p=out;p<len1;p++)
{
strcpy(sub_str[p],sub_str[p+1]);
}
len1--;
}
else
{
out++;
}
}
for(in=0;in<len1;in++)
{
printf("%s ",sub_str[in]);
}
}
Enter your String:C STRING PROGRAM PROGRAM STRING C
After removing duplicate words from a given String is:C STRING PROGRAM
Program in C++
Here is the source code of the C++ Program to Remove duplicate words from a given string.
Code:
#include<iostream>
#include <cstring>
using namespace std;
main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int in=0,out=0,p=0,len1=0;
char sub_str[100][100]={0};
//splitting Input String into sub string
while(str[p]!='\0')
{
out=0;
while(str[p]!=' ' && str[p]!='\0')
{
sub_str[in][out]=str[p];
p++;
out++;
}
sub_str[in][out]='\0';
in++;
if(str[p]!='\0')
{
p++;
}
}
cout<<"After removing duplicate words from a given String is:";
len1=in;
for(in=0;in<len1;in++)
{
for(out=in+1;out<len1;)
if(strcmp(sub_str[in],sub_str[out])==0)
{
for(p=out;p<len1;p++)
{
strcpy(sub_str[p],sub_str[p+1]);
}
len1--;
}
else
{
out++;
}
}
for(in=0;in<len1;in++)
{
cout<<sub_str[in]<<" ";
}
}
Enter your String:c++ string question question string c++
After removing duplicate words from a given String is:c++ string question
Program in Java
Here is the source code of the Java Program to Remove duplicate words from a given string.
Code:
import java.util.Scanner;
public class RemovDuplicateWords {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
int in=0,out=0,len1,p;
//splitting Input String into sub string
String[] sub_str=str.split("\\s");
System.out.print("After removing duplicate words from a given String is:");
len1=sub_str.length;
for(in=0;in<len1;in++)
{
for(out=in+1;out<len1;)
{
if((sub_str[in].compareTo(sub_str[out]))==0)
{
for(p=out;p<=len1;p++)
{
if(p>=p+1)
sub_str[p]=sub_str[p+1];
}
len1--;
}
else
{
out++;
}
}
for(in=0;in<len1;in++)
{
System.out.print(sub_str[in]+" ");
}
}
cs.close();
}
}
Enter your String:
csinfo360 csinfo360
After removing duplicate words from a given String is:csinfo360
Program in Python
Here is the source code of the Python Program to Remove duplicate words from a given string.
Code:
str=input("Enter Your String:")
sub_str=str.split(" ")
len1=len(sub_str)
print("After removing duplicate words from a given String is:")
for inn in range(len1):
out=inn+1
while out<len1:
if sub_str[out].__eq__(sub_str[inn]):
for p in range(out,len1+1):
if p >= p + 1:
sub_str[p]=sub_str[p+1]
len1-=1
else:
out+=1
for inn in range(len1):
print(sub_str[inn],end=" ")
Enter Your String:python string program string program
After removing duplicate words from a given String is:
python string program
0 Comments
Please do not Enter any spam link in the comment box