You are not logged in.

#26 2009-06-10 13:53:56

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: String Trim in C

@vacant: that gave me an idea for a single loop continued multiple times with gotos, removes only ' ' EDIT: actually this was the same as a infinite loop, like this:

#include <stdio.h>
#include <string.h>

char *trim(char *buffer){
 char *start;
 char *end;
 int right=0;
 while(1) {
 while(*(buffer++)==' ');
 
 if(right==0) {
  right=1;
  start=buffer-1;
 }
 else {
  if(*(buffer-1)=='\0') {
   if(*(buffer-2)==' ')
    *end='\0';
   return start;
  }
  else
   end=buffer;
 }
 }
}

void main() {
 char buffer[] = " Data";
 printf(">%s<\n", trim(buffer));
}

Last edited by Procyon (2009-06-10 14:00:40)

Offline

#27 2009-06-10 14:29:32

tom5760
Member
From: Philadelphia, PA, USA
Registered: 2006-02-05
Posts: 283
Website

Re: String Trim in C

Aprz wrote:

As for the whole true and false thing, what about bit-fields? tongue

Edit: I've never used a bit-field before, but just remember reading about it.

struct boolean {
    unsigned false:    0;
    unsigned true:     1;
} bool;

Wouldn't that be smaller than using an intenger?

Sorta off topic but, bool has been added in C99.

#include <stdbool.h>
#include <stdio.h>

int main() {
    bool foo = true;
    printf("Foo is %i\n", foo);
}

Compile with gcc --std=gnu99, and you have booleans!

Offline

#28 2009-06-10 20:05:57

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: String Trim in C

Good to see some other implementations in this thread, I think although the general process is the same we all implement it a different way smile

Offline

#29 2009-06-10 23:01:05

gnud
Member
Registered: 2005-11-27
Posts: 182

Re: String Trim in C

Guys, don't worry about 'memory waste' by not using booleans.
When working with the variables, it gets loaded into a register anyway, and int's fit into the register.
When not working with them (but storing them), if those 3 bytes really matter, then you might have bigger problems.

Offline

#30 2015-09-16 20:17:28

joaorsilva
Member
Registered: 2015-09-16
Posts: 1

Re: String Trim in C

In the case of trim this should work fine

char* trim(char * str) {
   
    int len = strlen(str);
    int start = 0;
    int pos = len-1;
   
    while(str[pos] == '') {
        pos--;
    }
    str[pos+1] = '\0';
   
    pos =0;
    while(str[pos] == '') {
        pos++;
    }
    return strcpy(str,str[pos]);
}

The problem is that if the string was dynamically allocated and you move the beginning of the string and the try to free it, you end up with a memory leak.

Offline

#31 2015-09-17 10:10:27

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,858
Website

Re: String Trim in C

Holy necrobump, batman. This is a six year old thread. I think it has served its purpose.

Closing.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB