Remove duplicate characters from a given string

Problem statement:- Program to Remove duplicate characters from a given string.

Example:-

              Input: Given

                                 String=CPP STRING PROGRAM

             Output: CP STRING OAM

            Input: Given

                                String=c++ string question

             Output: c+ string queo

Data requirement:-


   Input Data:- str

  Output Data:-
str

  Additional Data:- 
i, len, arr

Program in C

Here is the source code of the C Program to Remove duplicate characters from a given string.

Code:

#include<stdio.h>
#include<string.h>
main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int arr[256]={0},i;
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]!=' ')
        arr[str[i]]++;
    }
    printf("After Removing Duplicate character from a given string is:");
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]!=' '){
        if(arr[str[i]] !=0)
        {
            printf("%c",str[i]);
            arr[str[i]]=0;
        }}
        else
        {
            printf("%c",str[i]);
        }
    }
}

Input/Output:
Enter your String:CPP STRING PROGRAM
After Removing Duplicate character from a given string is:CP STRING OAM

Program in C++

Here is the source code of the C++ Program to Remove duplicate characters from a given string.

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int in,len=0,i,max=-1;
    char sub_str[100][100]={0};
    for(in=0;str[in]!='\0';in++)
    {
        len++;
    }
    int arr[256]={0};
    for(i=0;i<len;i++)
    {
        if(str[i]!=' ')
        arr[str[i]]++;
    }
    cout<<"After Removing Duplicate character from a given string:";
    for(i=0;i<len;i++)
    {
        if(str[i]!=' '){
        if(arr[str[i]]!=0)
        {
            cout<<str[i];
            arr[str[i]]=0;
        }}
        else
        {
            cout<<str[i];
        }
    }
}

Input/Output:
Enter your String:c++ string question
After Removing Duplicate character from a given string:c+ string queo

Program in Java

Here is the source code of the Java Program to Remove duplicate characters from a given string.

Code:

import java.util.Scanner;
public class RemovingDuplicateCharacter {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
int i;
    int arr[]=new int[256];
    for(i=0;i<str.length();i++)
    {
        if(str.charAt(i)!=' ')
            arr[str.charAt(i)]++;
    }
    System.out.print("After Removing Duplicate character from a given string is:");
    for (i =0; i <str.length(); i++) {
    if(str.charAt(i)!=' '){
    if(arr[str.charAt(i)]!=0){
    System.out.print(str.charAt(i));
    arr[str.charAt(i)]=0;
   }
  }
    else
    {
    System.out.print(str.charAt(i));
    }
    }
    cs.close();
}
}

Input/Output:
Enter your String:
removing Duplicate character
After Removing Duplicate character from a given string is:removing Duplcat h

Program in Python

Here is the source code of the Python Program to Remove duplicate characters from a given string.

Code:

str=input("Enter Your String:")
arr=[0]*256
for i in range(len(str)):
    if str[i]!=' ':
        num=ord(str[i])
        arr[num]+=1
print("After Removing Duplicate character from a given string is:")
for i in range(len(str)):
    if str[i]!=' ':
        if arr[ord(str[i])] !=0:
            print(str[i],end="")
            arr[ord(str[i])]=0
    else:
        print(str[i], end="")

Input/Output:
Enter Your String:python string question
After Removing Duplicate character from a given string is:
python srig que

Post a Comment

0 Comments