# Thread: recursive remove duplicates from a string

1. ## recursive remove duplicates from a string

i need to build:
void remove_duplicates(char string[], int index)

which removes chars that already appeared in the string before.

for example:
"aatbacedbc" ==> "atbced"

i am not allowed to use pointers , malloc , loops,external math functions.
i am allowed to use external recursive function.

i cant figure out an algorithm to do this.

i tried this
but its only a blind shot

how to build it

??
Code:
```void remove_duplicates(char string[], int index)
{
char ch;
if (string[index]=='\0')
{
return 1;
}
if(string[index]!=string[index+1])
{
remove_duplicates( string,index+1);
}
else
{
return 0;
}
}```

2. You can use a recursive function to make a sort of infinite loop, which is probably the point since you can't use a loop otherwise:

Code:
```#include <stdio.h>
#include <string.h>

char output[64]={'\0'};

void recurse (char *string, int count, int len) {
static int i=-1, found=0;
if (i==-1) {i++;recurse(string, 0, len); return;}
if (len==i) return;
if (output[count]==string[i]) { i++;
recurse(string,0,len);
return;}
if (count==found) {output[found]=string[i];
found++; i++;
recurse(string,0,len);
return;}
recurse(string,count+1,len);
}

int main (int argc, char *argv[]) {
int len;
if ((argc<2) || ((len=strlen(argv[1]))>63)) return 0;
recurse(argv[1],0,len);
printf("%s\n",output);
}```
Output: ajdswefv

3. i cant add a length ass parameter
only
(char string[], int index)

how to substitute it?

4. can i substitute
Code:
` if (len==i) return;`
with
Code:
` if (string[i]=='\0') return;`
and to remove the len parameter
??

5. Originally Posted by transgalactic2
i cant add a length ass parameter
only
(char string[], int index)

how to substitute it?
You could make len a global int, which would simplify the code.

6. Originally Posted by transgalactic2
can i substitute

Code:
` if (string[i]=='\0') return;`
I wouldn't do that unless you also make sure to add a \0 somewhere, since there are no string functions called so no auto line terminating...which suddenly makes me realize I got lucky because the output from that post wasn't null terminated! I will have to go back and change that...

7. char string[] is a pointer, its just a non-standard way of writing char* string. If you cant use pointers, you are SOL, since there is no way to refernce the data pointed to by a pointer without actually using the pointer.

8. i tried to run it
i cant use global variables

i get an error because there is no array called output.

Code:
```#include <stdio.h>
#include <string.h>

void recurse (char *string, int count, int len);
int main() {
char input[255];
char input2[255];
char result[510];
int index,flag,ch;
printf("enter the first string \n");
for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
{
input[index] = ch;
}

input[index] = '\0';

recurse (input, 0, index);

return 0;
}

void recurse (char *string, int count, int len) {
static int i=-1, found=0;
if (i==-1) {i++;recurse(string, 0, len); return;}
if (len==i) return;
if (output[count]==string[i]) { i++;
recurse(string,0,len);
return;}
if (count==found) {output[found]=string[i];
found++; i++;
recurse(string,0,len);
return;}
recurse(string,count+1,len);
}```

9. Originally Posted by transgalactic2
i cant use global variables
Then you could either:
• put strlen into the recurse function, or
• look for the '\0' at the end of the string

The 2nd option is much more efficient.

To avoid the "output string", print the output from within recurse() one character at a time rather than putting it into output.

10. how to replace this output[count] expression

Code:
```#include <stdio.h>
#include <string.h>

void recurse (char *string, int count);
int main() {
char input[255];
char input2[255];
char result[510];
int index,flag,ch;
printf("enter the first string \n");
for (index = 0; index < 254 && (ch = getchar()) != '\n' && ch >=0; ++index)
{
input[index] = ch;
}

input[index] = '\0';

recurse (input, 0);

return 0;
}

void recurse (char *string, int count) {
static int i=-1, found=0;
if (i==-1) {i++;recurse(string, 0); return;}
if (string[i]=='\0') return;
if (output[count]==string[i]) { i++;  //how to replace this output[count] expression
recurse(string,0);
return;}
if (count==found) {printf("%c",string[i]);
found++; i++;
recurse(string,0);
return;}
recurse(string,count+1);
}```

11. this program doesnt let me input a string
it just say
"press any key to continue"
and exits
??

Code:
```#include <stdio.h>
#include <string.h>

char output[64]={'\0'};

void recurse (char *string, int count, int len) {
static int i=-1, found=0;
if (i==-1) {i++;recurse(string, 0, len); return;}
if (len==i) return;
if (output[count]==string[i]) { i++;
recurse(string,0,len);
return;}
if (count==found) {output[found]=string[i];
found++; i++;
recurse(string,0,len);
return;}
recurse(string,count+1,len);
}

int main (int argc, char *argv[]) {
int len;
if ((argc<2) || ((len=strlen(argv[1]))>63)) return 0;

recurse(argv[1],0,len);
printf("%s\n",output);
}```

12. how to fix it

where do i put a string in there?

13. it doesnt let me input

14. if you're not allowed to use pointers, why do you use them?

before i'll try to write something, let me make sure i understand, your assignment is to use only pure recursive functions and nothing else?

15. i am trying to get it running and then when ill get a working version

ill change the pointers

yes pure recurtion
no pointers
no malloc
no global variable

but external help functions are alowed