You are not logged in.
@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
As for the whole true and false thing, what about bit-fields?
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
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
Offline
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
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
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