|
-
Aug 22nd, 2001, 12:28 PM
#1
Thread Starter
Hyperactive Member
Remove spaces from a string
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.
-
Aug 22nd, 2001, 12:40 PM
#2
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;
}
-
Aug 22nd, 2001, 01:19 PM
#3
transcendental analytic
way too heavy
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;
}
Last edited by kedaman; Aug 22nd, 2001 at 01:29 PM.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 01:56 PM
#4
WOW!
Cool solution but hard to understand.
-
Aug 22nd, 2001, 02:04 PM
#5
transcendental analytic
it definitely kicks ass 
It does a single loop, with two variables,one dropping the spaces and on going straight trough assigning the earlier one.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 02:34 PM
#6
Thread Starter
Hyperactive Member
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;
}
-
Aug 22nd, 2001, 02:44 PM
#7
transcendental analytic
PHP 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;
}
okay but you need to pass the termination pointer as well
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 02:47 PM
#8
Thread Starter
Hyperactive Member
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.
-
Aug 22nd, 2001, 02:53 PM
#9
transcendental analytic
*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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 03:01 PM
#10
Thread Starter
Hyperactive Member
I'm not using C++
I'm using an OLD C compiler that's not even on PC but on VAX.
-
Aug 22nd, 2001, 03:09 PM
#11
transcendental analytic
Ah, but that still shouldn't be a problem, I don't know C but the syntax of dereferencing shouldn't differ from C++
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 22nd, 2001, 03:34 PM
#12
Hmmm.. Tomexx, have you tried my code?
Does it work?
-
Aug 22nd, 2001, 06:58 PM
#13
Thread Starter
Hyperactive Member
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).
-
Aug 22nd, 2001, 09:12 PM
#14
Tomexx:
Do you want the code to remove ALL the spaces from a string or only the left-most and right-most spaces?
-
Aug 23rd, 2001, 01:30 AM
#15
transcendental analytic
If thats the case i've posted my solution for it here:
http://www.vbforums.com/showthread.p...threadid=97909
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 23rd, 2001, 01:49 AM
#16
I tried my code on various strings and it works perfectly.
The problem could only be that it doesn't achieve your goal.
-
Aug 23rd, 2001, 01:52 AM
#17
transcendental analytic
It's awfully slow though I think it's the compiler
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 23rd, 2001, 02:33 AM
#18
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. :
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);
}
It is possible to change the function so it changes the original string passed to it, if you want.
Anyway, bug fixed.
-
Aug 23rd, 2001, 02:38 AM
#19
transcendental analytic
It's still freaking heavy
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 23rd, 2001, 02:40 AM
#20
I know that.
It's not a contest kedaman. Tomexx can choose the code that fits him best.
-
Aug 23rd, 2001, 02:42 AM
#21
transcendental analytic
you're right, he'll end up choosing code that someone codes in C for him
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 23rd, 2001, 02:44 AM
#22
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|