The following code I found on tutorialgateway is used to remove all duplicate characters from a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, j, k;
printf("\n Please Enter any String : ");
gets(str);
for(i = 0; i < strlen(str); i++)
{
for(j = i + 1; str[j] != '\0'; j++)
{
if(str[j] == str[i])
{
for(k = j; str[k] != '\0'; k++)
{
str[k] = str[k + 1];
}
}
}
}
printf("\n The Final String after Removing All Duplicates = %s \n", str);
return 0;
}
I thought if I changed the == for != on the if statement it would instead remove all unique or non duplicate characters, but instead it does something else. What am I not understanding about how this code works?
Your solution will take approximately O(n^3).
Also, having three nested loops adds needless complexity.
If we change the algorithm to use a frequency table, we can reduce complexity. And, we can reduce the running time to 2*O(n) or O(n).
Side note: Never use gets. See: Why is the gets function so dangerous that it should not be used?
Here is the refactored code:
#include <stdio.h>
#include <string.h>
int
main()
{
char str[100];
char freq[256] = { 0 };
char *src;
char *dst;
unsigned char chr;
printf("\n Please Enter any String : ");
fgets(str,sizeof(str),stdin);
str[strcspn(str,"\n")] = 0;
printf("\n The Original String = %s \n", str);
// get frequency count
src = str;
for (chr = *src++; chr != 0; chr = *src++) {
switch (freq[chr]) {
case 0: // new [unique] char
freq[chr] = 1;
break;
default: // duplicate
freq[chr] = 2;
break;
}
}
dst = str;
src = str;
// copy over chars that are unique
for (chr = *src++; chr != 0; chr = *src++) {
if (freq[chr] == 1)
*dst++ = chr;
}
*dst = 0;
printf("\n The Final String after Removing All Duplicates = %s \n", str);
return 0;
}
Here is the program output:
Please Enter any String :
The Original String = abbbbbbbbbccdefghe
The Final String after Removing All Duplicates = adfgh
Related
Overall the code is working except one part.
The code first outputs sorted string in alphabetical order. Then the repeated characters are removed so each letter is displayed just once. My problem here is when I input "datastructures" it displays acdersttu, but instead I should have acderstu, which means with only one t. Where is the problem?
My code:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[100];
int freq[256] = {0};
char temp;
int i, j, k;
printf("\nEnter the string: ");
scanf("%s",str);
int n = strlen(str);
for (i = 0; i < n; i++) {
for (j = i+1; j < n; j++) {
if (str[i] > str[j]) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
printf("The sorted string is: %s", str);
for(i = 0; i < n; i++)
for(j = i + 1; str[j] != '\0'; j++)
if(str[j] == str[i])
for(k = j; str[k] != '\0'; k++)
str[k] = str[k + 1];
printf("\nThe sorted string after removing same characters is: %s ", str);
return 0;
}
The rule which you defined is applicable only when there are 2 repeated characters, since 3 t's are there it is not working fine. Below code is the change, for the second loop.
for(i = 0; i < n; i++)
for(j = i + 1; str[j] != '\0'; j++)
if(str[j] == str[i]){
for(k = j; str[k] != '\0'; k++)
str[k] = str[k + 1];
j=j-1;
}
printf("\nThe sorted string after removing same characters is: %s ", str);
return 0;
An alternative way to remove adjacent duplicate characters uses a single loop instead of three nested loops:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Remove adjacent duplicate characters */
static void remove_duplicates(char *str)
{
char *src = str;
char *dst = str;
char c = *dst = *src++;
while (c != '\0')
{
c = *src++;
if (c != *dst)
*++dst = c;
}
}
/* Comparison function for qsort() - comparing characters */
static int cmp_char(const void *v1, const void *v2)
{
int c1 = (unsigned char)*(const char *)v1;
int c2 = (unsigned char)*(const char *)v2;
return c1 - c2;
}
static void test(char *str)
{
size_t length = strlen(str);
printf("Before sort (%zu): [%s]\n", length, str);
qsort(str, length, sizeof(char), cmp_char);
printf("After sort (%zu): [%s]\n", strlen(str), str);
remove_duplicates(str);
printf("After removing duplicates (%zu): [%s]\n", strlen(str), str);
}
int main(void)
{
char str1[] = "datastructures";
test(str1);
char str2[] = "aaaaaaaaaaa";
test(str2);
char str3[] = "dbca";
test(str3);
char str4[] = "z";
test(str4);
char str5[] = "";
test(str5);
return 0;
}
Output
Before sort (14): [datastructures]
After sort (14): [aacderrsstttuu]
After removing duplicates (8): [acderstu]
Before sort (11): [aaaaaaaaaaa]
After sort (11): [aaaaaaaaaaa]
After removing duplicates (1): [a]
Before sort (4): [dbca]
After sort (4): [abcd]
After removing duplicates (4): [abcd]
Before sort (1): [z]
After sort (1): [z]
After removing duplicates (1): [z]
Before sort (0): []
After sort (0): []
After removing duplicates (0): []
I am creating a function that creates a username. The user is asked for first name then last name. The last name is concatenated to the first letter of the first name. Also, any upper case letter should be transformed into lowercase letters. So input 'John DoE' should equal 'jdoe'.
Another function counts the number of lowercase letters in the string "THiS SentENCE HAS SOMe LoWEr CASE ChARAcTERs". Lower case letters are then converted to upper case and the string is printed.
The first function will not convert the uppercase letters to lowercase. The second function does not even recognize the ASCII value of any character.
I've tried using pointers for the two functions but there was no change in output.
void createUsername()
{
int j = 1;
char firstName[15], lastName[15], userName[20];
printf("Enter your first name : ");
fgets(firstName, 15, stdin);
firstName[strlen(firstName) - 1] = '\0';
printf("Enter your last name : ");
fgets(lastName, 15, stdin);
lastName[strlen(lastName) - 1] = '\0';
userName[0] = tolower(firstName[0]);
for (int i = 0, j; lastName[i] != '\0'; i++, j++)
userName[j] = tolower(lastName[i]);
userName[j+1] = '\0';
printf("User name : %s", userName);
}
void lowerToUpperCase()
{
char sentence[] = "THiS SentENCE HAS SOMe LoWEr CASE ChARAcTERs";
int lowerCases = 0;
for (int i = 0; sentence[i] != '\0'; i++)
{
if ((sentence[i] >= 97) && (sentence[i] <= 122))
{
lowerCases++;
sentence[i] = toupper(sentence[i]);
}
}
printf("Number of lower case characters= %d \n", lowerCases);
printf("Upper case sentence : %s \n", sentence);
}
expected output of Joe SMITH should be jsmith. The number of lower case letters should be 10 and the sentence should print all uppercase.
The actual output gives JSMITH, the number of lowercase letters is 0 and the sentence prints the original.
Lets start with simple things and we will go from there:
You can easily compare a with sentence[i]. Do this:
if ((sentence[i] >= 'a') && (sentence[i] <= 'z'))
{
lowerCases++;
sentence[i] = toupper(sentence[i]);
}
than comes this bit:
i < lastName[i] != '\0'
What the hell on earth is this? If you take a look at this page:
https://en.cppreference.com/w/c/language/operator_precedence
you will see that < has precedence over !=.
EDIT:
for (int i = 0, j; lastName[i] != '\0'; i++, j++)
j is reinitialized here as local variable for for() loop so it might get 0 but in my case it always got some garbage and setfault. So put this:
for (int i = 0; lastName[i] != '\0'; i++, j++)
and you will be set to go.
Apart from the problems mentioned in other answers, you also have problems with memory overwrites. Typing names longer than the buffers cause great pain in production. Better allocate enough memory on the fly to accommodate the names.
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void createUsername() {
printf("Enter your first name : ");
char *firstName;
errno = 0;
int n = scanf("%ms", &firstName);
if (n != 1) {
perror("scanf");
exit(EXIT_FAILURE);
}
printf("Enter your last name : ");
char *lastName;
errno = 0;
n = scanf("%ms", &lastName);
if (n != 1) {
perror("scanf");
free(firstName);
exit(EXIT_FAILURE);
}
char *userName = (char *) malloc(1u + strlen(lastName) + 1u);
userName[0] = tolower(firstName[0]);
char *userNameIter = userName + 1;
char *lastNameIter = lastName;
while ((*userNameIter = tolower((unsigned char) *lastNameIter))) {
userNameIter++;
lastNameIter++;
}
printf("User name : %s\n", userName);
free(userName);
free(lastName);
free(firstName);
}
void lowerToUpperCase() {
char sentence[] = "THiS SentENCE HAS SOMe LoWEr CASE ChARAcTERs";
int lowerCases = 0;
for (int i = 0;; i++) {
unsigned char const ch = (unsigned char) sentence[i];
if (islower(ch)) {
lowerCases++;
sentence[i] = toupper(ch);
} else if (ch == '\0') {
break;
}
}
printf("Number of lower case characters = %d\n", lowerCases);
printf("Upper case sentence : %s\n", sentence);
}
So I have an assignment where I should delete a character if it has duplicates in a string. Right now it does that but also prints out trash values at the end. Im not sure why it does that, so any help would be nice.
Also im not sure how I should print out the length of the new string.
This is my main.c file:
#include <stdio.h>
#include <string.h>
#include "functions.h"
int main() {
char string[256];
int length;
printf("Enter char array size of string(counting with backslash 0): \n");
/*
Example: The word aabc will get a size of 5.
a = 0
a = 1
b = 2
c = 3
/0 = 4
Total 5 slots to allocate */
scanf("%d", &length);
printf("Enter string you wish to remove duplicates from: \n");
for (int i = 0; i < length; i++)
{
scanf("%c", &string[i]);
}
deleteDuplicates(string, length);
//String output after removing duplicates. Prints out trash values!
for (int i = 0; i < length; i++) {
printf("%c", string[i]);
}
//Length of new string. The length is also wrong!
printf("\tLength: %d\n", length);
printf("\n\n");
getchar();
return 0;
}
The output from the printf("%c", string[i]); prints out trash values at the end of the string which is not correct.
The deleteDuplicates function looks like this in the functions.c file:
void deleteDuplicates(char string[], int length)
{
for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length;)
{
if (string[j] == string[i])
{
for (int k = j; k < length; k++)
{
string[k] = string[k + 1];
}
length--;
}
else
{
j++;
}
}
}
}
There is a more efficent and secure way to do the exercise:
#include <stdio.h>
#include <string.h>
void deleteDuplicates(char string[], int *length)
{
int p = 1; //current
int f = 0; //flag found
for (int i = 1; i < *length; i++)
{
f = 0;
for (int j = 0; j < i; j++)
{
if (string[j] == string[i])
{
f = 1;
break;
}
}
if (!f)
string[p++] = string[i];
}
string[p] = '\0';
*length = p;
}
int main() {
char aux[100] = "asdñkzzcvjhasdkljjh";
int l = strlen(aux);
deleteDuplicates(aux, &l);
printf("result: %s -> %d", aux, l);
}
You can see the results here:
http://codepad.org/wECjIonL
Or even a more refined way can be found here:
http://codepad.org/BXksElIG
Functions in C are pass by value by default, not pass by reference. So your deleteDuplicates function is not modifying the length in your main function. If you modify your function to pass by reference, your length will be modified.
Here's an example using your code.
The function call would be:
deleteDuplicates(string, &length);
The function would be:
void deleteDuplicates(char string[], int *length)
{
for (int i = 0; i < *length; i++)
{
for (int j = i + 1; j < *length;)
{
if (string[j] == string[i])
{
for (int k = j; k < *length; k++)
{
string[k] = string[k + 1];
}
*length--;
}
else
{
j++;
}
}
}
}
You can achieve an O(n) solution by hashing the characters in an array.
However, the other answers posted will help you solve your current problem in your code. I decided to show you a more efficient way to do this.
You can create a hash array like this:
int hashing[256] = {0};
Which sets all the values to be 0 in the array. Then you can check if the slot has a 0, which means that the character has not been visited. Everytime 0 is found, add the character to the string, and mark that slot as 1. This guarantees that no duplicate characters can be added, as they are only added if a 0 is found.
This is a common algorithm that is used everywhere, and it will help make your code more efficient.
Also it is better to use fgets for reading input from user, instead of scanf().
Here is some modified code I wrote a while ago which shows this idea of hashing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUMCHAR 256
char *remove_dups(char *string);
int main(void) {
char string[NUMCHAR], temp;
char *result;
size_t len, i;
int ch;
printf("Enter char array size of string(counting with backslash 0): \n");
if (scanf("%zu", &len) != 1) {
printf("invalid length entered\n");
exit(EXIT_FAILURE);
}
ch = getchar();
while (ch != '\n' && ch != EOF);
if (len >= NUMCHAR) {
printf("Length specified is longer than buffer size of %d\n", NUMCHAR);
exit(EXIT_FAILURE);
}
printf("Enter string you wish to remove duplicates from: \n");
for (i = 0; i < len; i++) {
if (scanf("%c", &temp) != 1) {
printf("invalid character entered\n");
exit(EXIT_FAILURE);
}
if (isspace(temp)) {
break;
}
string[i] = temp;
}
string[i] = '\0';
printf("Original string: %s Length: %zu\n", string, strlen(string));
result = remove_dups(string);
printf("Duplicates removed: %s Length: %zu\n", result, strlen(result));
return 0;
}
char *remove_dups(char *str) {
int hash[NUMCHAR] = {0};
size_t count = 0, i;
char temp;
for (i = 0; str[i]; i++) {
temp = str[i];
if (hash[(unsigned char)temp] == 0) {
hash[(unsigned char)temp] = 1;
str[count++] = str[i];
}
}
str[count] = '\0';
return str;
}
Example input:
Enter char array size of string(counting with backslash 0):
20
Enter string you wish to remove duplicates from:
hellotherefriend
Output:
Original string: hellotherefriend Length: 16
Duplicates removed: helotrfind Length: 10
i am trying to write a program which reverses a entire string and also may print reversing each word of the string more than one time.
For example my string is:
"2
Fox jumps over the lazy dog."
for this, the output should be:
.god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF.
I am trying to store each word in a 2d array and then reverse each word but i am not able to do that.
here is my code. kindly help
Note: how do we provide EOF in console
#include <stdio.h>
#include <stdlib.h>
int main() {
char string[100][100];
char ch;
int i = 0, j = 0, l = 0, count = 0, x = 0, y = 0;
while ((ch = getchar()) != EOF) {
string[i][l++] = ch;
if (ch == ' ') {
string[i][l] = '\n';
i++;
l = 0;
count++;
}
}
for (x = 0; x <= count; x++) {
int length = strlen(string[x]) - 1;
for (y = length; y >= 0; --y)
printf("%s", string[y]);
}
return 0;
}
Here are a few changes to your code check the comments for explanation.
int main()
{
char string[100][100];
char ch;
int i=0,j=0, l=0, count=0, x=0, y=0;
while((ch=getchar())!=EOF)
{
string[i][l++] = ch;
if(ch==' ')
{
string[i][l] = '\0'; //make the string null terminated otherwise you cant work with its length
i++;
l=0;
count++;
}
}
string[i][l]='\0'; //make the last string null terminated
for(x=count; x>=0; x--) //read from last counter
{
int length = strlen(string[x])-1;
for(y=length; y>=0; --y)
{
printf("%c", string[x][y]); //print by each character and not string.
}
}
return 0;
}
Corrections in your code:
C strings are null terminated, but you are terminating your strings with newline \n character, which is wrong.
You are storing the whitespace with the string, reversing will be difficult in this case.
Your print statement won't reverse the string, print it character by character.
For the output that you need, you can consider this code.
#include <stdio.h>
#include <stdlib.h>
int main() {
char string[100][100];
char ch;
int i = 0, j = 0, l = 0, count = 0, x = 0, y = 0;
while ((ch = getchar()) != EOF) {
if (ch == ' ') {
string[i][l] = '\0'; /// Null terminate the string
i++;
l = 0;
count++;
}
else
string[i][l++] = ch; /// Don't add whitespace to the string
}
string[i][l] = '\0';
for (x = count; x >= 0; x--) {
int length = strlen(string[x]) - 1;
for (y = length; y >= 0; --y)
printf("%c", string[x][y]); /// Print the string in reverse
printf(" ");
for (y = length; y >= 0; --y)
printf("%c", string[x][y]); /// Twice
printf(" ");
}
return 0;
}
Input
2 Fox jumps over the lazy dog.
Output
.god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF 2 2
See http://ideone.com/qaIoW9
If I understand you want to duplicate each reversed word N times as specified by the first number in the string, then something like the following would work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strrevdup (char* str);
int main (int argc, char **argv) {
if (argc < 2 ) {
fprintf (stderr, "error: insufficient input, usage: %s \"# string\"\n",
argv[0]);
return 1;
}
char *str = strdup (argv[1]);
char *p = str;
char *rev = NULL;
int mult = 0;
int i = 0;
while (*p && *p != ' ') p++;
*p = 0;
mult = atoi (str);
*p = ' ';
if (!mult) return 1;
while (*p && *p == ' ') p++;
rev = strrevdup (p);
char *r = rev;
printf ("\n the reversed string with duplicated words '%d' times is:\n\n", mult);
for (p = strtok (r, " "); p; p = strtok (NULL, " \n"))
for (i = 0; i < mult; i++)
printf (" %s", p);
printf ("\n\n");
free (str);
free (rev);
return 0;
}
/** strrevdup - reverse duplicate string, swaps src & dest each iteration.
* Takes valid string, duplicates and reverses, original is preserved.
* Returns pointer to reversed string on success, NULL otherwise.
* Requires string.h, caller is responsible for freeing memory allocated.
*/
char *strrevdup (char* str)
{
if (!str) {
printf ("%s() error: invalid string\n", __func__);
return NULL;
}
char *rstr = strdup (str);
char *begin = rstr;
char *end = rstr + strlen (rstr) - 1;
char tmp;
while (end > begin){
tmp=*end;
*end-- = *begin;
*begin++ = tmp;
}
return rstr;
}
Output
$ ./bin/strrevndup "2 Fox jumps over the lazy dog."
the reversed string with duplicated words '2' times is:
.god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF
you may try this code although it will reverse and print words in different lines.. you may try few more things to get the desired answer.
` #include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char string[1024][1024];
char ch;
int t,z;
scanf("%d",&t);
z=t;
int i=0, l=0, count=0, x=0, y=0;
getchar();
while((ch=getchar())!=EOF)
{
if(ch=='\n')
{
i++;
l=0;
count++;
string[i][l++] = ch;
i++;
l=0;
}
else if(ch==' ')
{
i++;
l=0;
count++;
}
else{
string[i][l++] = ch;
}
}
for(x=count+1; x>=0; x--)
{
if(string[x][0]=='\n')
{
printf("\n");
}
else{
char *rev=strrev(string[x]);
while(t--)
printf("%s ",rev);
t=z;
}
}
return 0;
}`
I wrote the following function for removing duplicate characters from a string..For ex: if
str = "heeello;
removeDuplicate(str)
will return helo...But it shows some error on runtime .I have added some printf() statements for debugging...Can anyone tell me what the problem is ?
char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in a string is not repeating
{
int i = 0,j;
char ch;
printf("\nstr is %s",str);
while((ch = str[i++] )!= '\0')
{
j = i;
printf("\n----ch = %c----",ch);
while(str[j] != '\0')
{
printf("\n--------Checking whether %c = %c \n",str[j],ch);
if(ch == str[j])
{
printf("\n------------Yes");
while(str[j]!='\0')
{
printf("\nRemoving %c %d -- \n",str[j]);
str[j] = str[++j];
--i;
}
break;
}
printf("\n------------No");
//printf("\njj");
j++;
}
}
return str;
}
You are passing a string literal, which you are not allowed to modify to this function, instead you should do:
char myStr[] = "heee";
removeDuplicate(myStr);
Also, please note that in the following lines your have to specifiers inside the printf (%c %d), but you pass only one argument (str[j]):
printf("\nRemoving %c %d -- \n",str[j]);
This may cause all sorts of bad things...
You should correct your code as follows:
In first while loop: j = i+1;
In third while loop: i--; // is not required
Remove that unwanted specifier form printf("Removing %d %d:",str[j])
Doing incorrectly :
str[j] = str[++j] // you are increasing j before assigning
str[j] = str[j++] // correct way to do.But it is compiler dependent i guess
Better to use:
t = j;
str[t] = str[++j];
I don't think this function does what you want. The remove loop is really fishy.. you decrement i which looks wrong.. and you increment j which is probably also wrong:
while(str[j]!='\0')
{
printf("\nRemoving %c %d -- \n",str[j]);
str[j] = str[++j]; // now the new character is at location j, but since
// you incremented j you can't access it anymore
--i; // why is i dependent on the remove stuff?
}
I would go for a simpler approach. Create a large bool array. Loop through your string and store whether you already encountered the current character or not. If not, print it.
Check the following code :
char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in a string is not repeating
{
int i = 0,j;
char ch;
int repIndex=0;
int temp=0;
printf("\nstr is %s",str);
while((ch = str[i++] )!= '\0')
{
j = i;
printf("\n----ch = %c----",ch);
while(str[j] != '\0')
{
printf("\n--------Checking whether %c = %c \n",str[j],ch);
repIndex = j;
if(ch == str[repIndex])
{
printf("\n------------Yes");
while(str[repIndex]!='\0')
{
printf("\nRemoving %c %d \n",str[j]);
temp = repIndex;
str[temp] = str[++repIndex];
}
} else { j++; }
}
}
return str;
}
int main ( int argc, char ** argv)
{
char myStr[]="asdfhelllasdfloofdoeohz";
printf ("OUtput is : %s \n", removeDuplicate(myStr) );
}
//removing the redundant characters in a string
#include<stdio.h>
int main()
{
int i=0,j,arr[26]={},temp; //array for hashing
char s[10],arr1[10],*p; //array 4 storing d output string
printf("Enter the string\n");
scanf("%s",s);
p=s;
while(*p!='\0')
{
temp=((*p)>92)?(*p)-'a':(*p)-'A'; //asuming lowr and upr letters are same
if(arr[temp]==0) //if it is not hashed ie if that char is not repeated
{
arr1[i]=temp+'a'; //return the string in lowecase
arr[temp]=1; //storing value so that this character sd not be placed again
i++;
}
p++; //else ignore the alphabet
}
for(j=0;j<i;j++)
{
printf("%c",arr1[j]); //print the string stored in arr1
}
return 0;
}
I have corrected the code as follows
char* removeDuplicate(char str[])//remove duplicate characters from a string,so that each character in a string is not repeating
{
int i = 0,j;
char ch;
while((ch = str[i++] )!= '\0')
{
j = i;
while(str[j] != '\0')
{
if(ch == str[j])
{
while(str[j]!='\0')
str[j] = str[++j];
i--;
break;
}
j++;
}
}
return str;
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char *str;
int count=0;
cout<<"enter the string which have repetative characters"<<endl;
cin>>str;
char *str2;
int m=0;
for(int i=0;i<=strlen(str);i++)
{
char ch=str[i];
if(i==0)
{
str2[m]=str[i];
m++;
}
for(int j=0;j<=strlen(str2);j++)
{
if(ch==str2[j])
count++;
}
if(count==0)
{
str2[m]=str[i];
m++;
}
count=0;
if(i==strlen(str))
str2[m]='\0';
}
puts(str2);
getch();
}
O(n) complexity
char *removeDuplicates(char *str){
int hash[256] = {0};
int currentIndex = 0;
int lastUniqueIndex = 0;
while(*(str+currentIndex)){
char temp = *(str+currentIndex);
if(0 == hash[temp]){
hash[temp] = 1;
*(str+lastUniqueIndex) = temp;
lastUniqueIndex++;
}
currentIndex++;
}
*(str+lastUniqueIndex) = '\0';
return str;
}
Refer: http://www.geeksforgeeks.org/remove-all-duplicates-from-the-input-string/