0

Possible Duplicate:
Im trying to make this code recursive but for some reason it doesnt work

Im trying to write program using RECURSION to change multiple spaces to one space can anyone help ? example "a_______b" changes to "a_b" it is a task that im trying to do for a long time ! can anyone help ?

here i tried this but i think the design doesnt work for recursion

void text_r(char *str)
{
    char *dst = str;

            if(*str=='\0')return ;
            *dst++ = *str;
            if (isspace(*str)) {
                    do ++str; while (isspace(*str));
                    --str;
            }
          return text_r(str++);
} 

i wrote the code without recursion but i have problem in converting it

void compress_spaces(char *str) { char *dst = str;

    for (; *str; ++str) {
            *dst++ = *str;
            if (isspace(*str)) {
                    do ++str; while (isspace(*str));
                    --str;
            }
    }
    *dst = 0;

}

Community
  • 1
  • 1
user2042145
  • 141
  • 1
  • 1
  • 9

3 Answers3

0

Not the best method but try something along these lines

char* remove_space(char *str)
{
    char *dst = str;

    while(*str!=' ' ||*str !='\0')
        *dst++ = *str;
    if (isspace(*str)) {
            do ++str; while (isspace(*str));
            --str;
    }
  return strcat(dst,remove_space(str++));
}

Idea being that you find characters and store them in a string and when you reach a space you store the first one and ignore the rest. Then you can send the new string to the function again. and you return the result concatinated with the new string

P.S. Probably the code above will not compile but it should give you a good idea on how to approach this.

Elaborating a bit:

make a function which saves all characters till a space, then ignores all consecutive spaces and sends the remaining string to a function which returns a clean string back. then it joins the two strings to make a bigger clean string.

Techmonk
  • 1,459
  • 12
  • 20
  • the idea was clear for me i even wrote it with out recursion but when i wanted to change it to a recursive way it didnt work im not so good in recursion , maybe i can post the code if that helps ? – user2042145 Feb 05 '13 at 08:56
  • if you have a working code with iteration post that... – Techmonk Feb 05 '13 at 09:01
  • you should add the char *dst as an argument to the function and not initialize it in the loop then it should work fine – Techmonk Feb 05 '13 at 09:57
0

Here's my implementation. I replace each band of multiple spaces by keeping of the last space character and make a remove that band when a nonspace character is found

void reduce(String s, int curIndex, int lastSpaceIndex)
    if (lastSpaceIndex != -1)
        if s[curIndex] is not a space
            then replace substring from s[lastSpaceIndex, curIndex-1] by a space
        else
            reduce(s, curIndex+1, lastSpaceIndex);
    else
        if s[curIndex] is not a space
            then reduce(s, curIndex+1, -1)
        else
            reduce(s, curIndex+1, curIndex)
Minh Pham
  • 948
  • 12
  • 22
  • dont you think that using pointers for the current index would be better ? i have the idea but i cant put it all together in a good code ! check my previous code can you spot the mistake there ? – user2042145 Feb 05 '13 at 09:23
0

Recursive version (avoiding any iterative part, like while) using the same pointer, given twice as argument

void recur(char *str, char *out) {
  if (*str!=' ' || str[1]!=' ') *out++ = *str;
  if (*str) recur(str+1, out);
}

Recursive version having only one param

void recur(char *str) {
  static char *out = NULL;
  if (out == NULL) out = str;
  if (*str!=' ' || str[1]!=' ') *out++ = *str;
  if (*str) recur(str+1);
}

Iterative version

void iter(char *str) {
  char *out = str;
  do {
    if (*str!=' ' || str[1]!=' ') *out++ = *str;
  } while (*str++);
}

To be called like

  char str[] = "   abc  d e  f   ";
  // either recursive
  recur(str, str);
  // or iterative
  iter(str);
Déjà vu
  • 28,223
  • 6
  • 72
  • 100