E
E
Eugene2015-04-01 21:15:34
C++ / C#
Eugene, 2015-04-01 21:15:34

How to write a function to remove extra spaces in a string?

The string is represented as a list. The first field of the list item is a string character. The second is a pointer to the next element of the list, or the NULL end of the list. The result list is formed by modifying the original list.
List item:

struct List
{
  char c;
  List *next;
};

Function to remove spaces:
List *delSpaces(List *p)
{
  p = delSpace(p);
  List *head = p;
  while (p)
  {
    p->next = delSpace(p->next);
    p = p->next;
  }
  return head;
}

This function only removes the spaces before the first word, the rest are left untouched. Please find the mistake. Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mrrl, 2015-04-01
@evlevin

Extra spaces are those of which there is more than one in a row?

List *delSpaces(List *p) {
    for(List **a=&p;*a;){
        if((*a)->c==' ' && (*a)->next && (*a)->next->c==' ') *a=(*a)->next;
        else a=&((*a)->next);
    }
    return p;
}

Not checked.

D
Denis, 2015-04-01
@denisk0n

The simplest thing that comes to mind:

List *delSpaces(List *p) {
    while (p && p->c == ' ')
    {
        p = p->next;
    }

    struct L *head = p;
    struct L *prev = p;
    p = p->next;

    while (p) 
    {
        if (p->c == ' ') 
        {
            prev->next = p->next;
        }
        else 
        {
            prev = p;
        }

        p = p->next;
    }

    return head;
}

PS If Cho, then 5 to me for the lab!

K
kozura, 2015-04-02
@kozura

I present my implementation.

struct Item {
  Item(): next(NULL), c('\0') {
  }

  char c;
  Item *next;
};

struct List {
  List(const char* in_str) : item(NULL){
    unsigned size = strlen(in_str);

    item = new Item[size];

    for (unsigned i(0); i < size; ++i) {
      item[i].c = in_str[i];
      
      if (i) {
        item[i-1].next = &item[i];
      }
    }
  }

  ~List() {
    delete[] item;
  }

  void remove(char in_) {
    Item* it = item;

    while (it) {
      if (it->c == in_) {
        *it = *it->next;
      }

      if (it->next) {
        if (it->c != in_) {
          it += (it->next - it);
        }
      } else {
        if (it->c == in_) {
          it->c = '\0';
        }
        break;
      }
    }
  }

  const char* takeString() {
    const unsigned size = 255;
    char str[size];

    Item* it = item;
    char* ptrstr = (char*)&str;

    memset(&str, '\0', size);

    while (it) {
      *ptrstr = it->c;

      if (!it->next) {
        break;
      }
          
      if (it->next) {
        it += (it->next - it);
        ++ptrstr;
      } else {
        break;
      }
    }

    return str;
  }

  Item* item;
};

int main()
{
  List list("    bla1         bla2  bla3    ");

  list.remove(' ');

  printf("\n begin[%s%s\n", list.takeString(), "]end");

  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question