Hi,
How can I remove spaces from strings?
I'm talking about good old C not C++.
Is there a built in function or do I have to write my own?
Please provide a sample.
Printable View
Hi,
How can I remove spaces from strings?
I'm talking about good old C not C++.
Is there a built in function or do I have to write my own?
Please provide a sample.
This works: :)
PHP Code:#include <iostream.h>
#include <string.h>
int main()
{
char myStr[] = "Blah blah yadda packa.";
unsigned int myLen = strlen(myStr);
for (unsigned int i = 0; i < myLen; i++)
{
if (myStr[i] == ' ')
{
for (unsigned int j = i; j < myLen; j++)
myStr[j] = myStr[j+1];
}
}
cout << myStr << endl;
return 0;
}
PHP Code:#include <iostream.h>
#include <string.h>
int main()
{
char myStr[] = "Blah blah yadda packa.";
char* i=myStr;
char* l=i+sizeof(myStr);
for (char* j=i;i<l;*j=*++i)j+=(*i!=32);
*j=0;
cout << myStr << endl;
return 0;
}
Cool solution but hard to understand. :cool:
it definitely kicks ass :cool:
It does a single loop, with two variables,one dropping the spaces and on going straight trough assigning the earlier one.
Thanks guys,
Kedaman, your code doesn't seem to work.
Could you take a look at it again and make it into a function that accepts the string as the argument.
Also where do you initialize the 'j' variable?
I want to be able to call it like:
Code:myString[] = "Bla Bla Bla "
Trim(myString)
void Trim(char* szStrToCheck)
{
return;
}
okay but you need to pass the termination pointer as wellPHP Code:#include "stdafx.h" //maybe it's because this wasnt included
#include <iostream.h>
#include <string.h>
void Trim(char*,char*);
int main()
{
char myStr[] = "Blah blah yadda packa.";
Trim(myStr,myStr+sizeof(myStr));
cout << myStr << endl;
return 0;
}
void Trim(char* i,char* l)
{
for (char* j=i;i<l;*j=*++i)j+=(*i!=32);
*j=0;
}
BTW,
I'm compiling the program on a VAX VMS compiler.
It gave me an error here:
'*j=*' saying that this is an obsolete statement or something
I'll try your new code.
*j=*++i;
'tis suppose to increment i pointer and assign it's contents to j's, maybe it'll work if you put parentesis around like this:
*j=*(++i);
but it's perfectly legal to do this in C++ so i wonder what kind of compiler wouldn't understand it.
I'm not using C++
I'm using an OLD C compiler that's not even on PC but on VAX.
Ah, but that still shouldn't be a problem, I don't know C but the syntax of dereferencing shouldn't differ from C++
Hmmm.. Tomexx, have you tried my code?
Does it work? :rolleyes:
Sc0rp,
Yes I did try it and it almost worked. There was a slight problem though, but I'll give you more details tomorrow (the program is at work).
Tomexx:
Do you want the code to remove ALL the spaces from a string or only the left-most and right-most spaces?
If thats the case i've posted my solution for it here:
http://www.vbforums.com/showthread.p...threadid=97909
I tried my code on various strings and it works perfectly.
The problem could only be that it doesn't achieve your goal.
It's awfully slow though :p I think it's the compiler
I found a flaw in my program.
It wouldn't remove multiple spaces that appear next to each other.
I fixed it and I also wrapped it in a function. :D :
It is possible to change the function so it changes the original string passed to it, if you want.PHP Code:#include <iostream.h>
#include <string.h>
int RemoveSpaces(char*, const char*);
unsigned int GetStrLen(const char*);
// Main program
int main()
{
char myStr[] = " This is a string with a lot of spaces. . ... ";
char* newStr = new char[GetStrLen(myStr)];
RemoveSpaces(newStr, myStr);
cout << newStr << endl;
delete[] newStr;
return 0;
}
// Removes all space from a string
int RemoveSpaces(char* newStr, const char* oldStr)
{
unsigned int theLen = strlen(oldStr);
strcpy(newStr, oldStr);
for (unsigned int i = 0; i < theLen; i++)
{
while (newStr[i] == ' ')
{
for (unsigned int j = i; j < theLen; j++)
newStr[j] = newStr[j + 1];
}
}
return 0;
}
// Returns the length of the string passed to it including the null char
unsigned int GetStrLen(const char* theStr)
{
return (strlen(theStr) + 1);
}
Anyway, bug fixed. :rolleyes:
It's still freaking heavy :p
I know that.
It's not a contest kedaman. Tomexx can choose the code that fits him best.
you're right, he'll end up choosing code that someone codes in C for him
:D :p :rolleyes: