0

I'm trying to make this code recursive but for some reason it doesn't work.

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

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

EDIT: I tried this:

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

            if(*str=='\0')return ;
            *dst++ = *str;
            if (isspace(*str)) {
                    do ++str; while (isspace(*str));
                    --str;
            }//Missing brace from orig is this ok?
          return text_r(str++);
}

Didn't work. Any ideas?

cs95
  • 379,657
  • 97
  • 704
  • 746
user2042145
  • 141
  • 1
  • 1
  • 9
  • 1
    I don't see any recursion in there ? – Paul R Feb 05 '13 at 07:16
  • 2
    I don't see any recurssion here. Show us the code that is not working and say **what** is not working in it. – Ivaylo Strandjev Feb 05 '13 at 07:16
  • yea, its *not* recursive. You need a call to `compress_spaces()` for that. – WhozCraig Feb 05 '13 at 07:16
  • it is a code to change multiple spaces to one space – user2042145 Feb 05 '13 at 07:16
  • a recursive function must call itself, so where is the "compress_spaces" call? – Miguel Prz Feb 05 '13 at 07:16
  • Recursion: See recursion, oops, there is none... – leppie Feb 05 '13 at 07:16
  • To change multiple consecutive spaces by one, try a regular expression like this: / +/ /g (2 spaces before the +) – Miguel Prz Feb 05 '13 at 07:18
  • i know it's not recursive i tried to make it recursive but it didnt work here is my try >>>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++); } – user2042145 Feb 05 '13 at 07:18
  • Did somebody mistaken re-entrant with recursive? – KBart Feb 05 '13 at 07:19
  • 1
    @user2042145 ouch. update *your question* with that please. don't post question code in a comment. – WhozCraig Feb 05 '13 at 07:19
  • i updated it ! any idea ppl – user2042145 Feb 05 '13 at 07:23
  • 3
    Out of curiosity, why are you trying to make it recursive? Is the program too fast or does it use too little memory? – Lundin Feb 05 '13 at 07:23
  • @Lundin LOL. best question I've seen on SO today. – WhozCraig Feb 05 '13 at 07:24
  • im trying to help my friend who is taking the course in C language i wrote the code but he needs a recursive one – user2042145 Feb 05 '13 at 07:25
  • @user2042145 you can start by telling your friend this is a *really* lousy question to try and teach students recursion, and his prof needs to think of better examples. This task is *ideal* for an iterative solution; recursion honestly just makes it muddy. – WhozCraig Feb 05 '13 at 07:27
  • The most common argument for using recursion seems to be "I'm trying to understand recursion". Rather than learning of how recursion works, you should perhaps focus on understanding what it is good for. – Lundin Feb 05 '13 at 07:32
  • Closed so cannot answer, but you could `void recur(char *str, char *out) { if (*str!=' ' || str[1]!=' ') *out++ = *str; if (*str) recur(str+1, out); }` Called with *str* and *out* being the same pointer (if you want) to a writable memory area (eg. `char str[] = "...";`) – Déjà vu Feb 05 '13 at 09:23
  • @ring0 check this link please – user2042145 Feb 05 '13 at 09:32
  • http://stackoverflow.com/questions/14702877/recursive-form-of-changing-multiple-spaces-to-one-space – user2042145 Feb 05 '13 at 09:32

2 Answers2

1

Your dst pointer is not the same pointer in the recursively called function, pass it as an argument instead.

void text_r(char *dst, char *str) {
  if (*str=='\0')
    return;
  *dst++ = *str;
  if (isspace(*str)
    while (isspace(*str++));
  else
    ++str;
  return text_r(dst, str);
}

Why you want to do this with recursion is completely beyond me by the way, it only wastes time and space.

wich
  • 16,709
  • 6
  • 47
  • 72
  • The code works perfectly ! try it ur self – user2042145 Feb 05 '13 at 07:29
  • @WhozCraig, user2042145 yeah sorry, wasn't reading the dst initialization correctly, sorry about that. The problem is still the same though, you reinitialize the dst pointer every time the function is called which messes up your algorithm, pull it onto the stack to fix it. – wich Feb 05 '13 at 07:36
  • That would defiantly work ! but here i trying to help some student and the task is to create a function with only the string in the stack ! i know that is kind of stupid but i cant do it :P – user2042145 Feb 05 '13 at 07:43
  • That won't work with this design. – wich Feb 05 '13 at 07:45
  • any ideas ? hints maybe ? – user2042145 Feb 05 '13 at 07:48
0

Perhaps this SO question may helps you. It resolves your problem with a regular expression

Community
  • 1
  • 1
Miguel Prz
  • 13,718
  • 29
  • 42