removing spaces-newbie
-
Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel
u can use strtok function to remove or find special character in a given string. using simlple logic, u can make it suit for ur purpose. strtok, wcstok, _mbstok[^] SaRath.
"Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++ -
Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel
use
[std::string::replace()](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp "New Window")]
instead...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
use
[std::string::replace()](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp "New Window")]
instead...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
good solution. thanks for the info :) SaRath.
"Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++ -
Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel
If you do not use MFC, you can use STL. You can read the file using a stream object like
ifstream
, store strings instring
object, and usefind
function to find a sequence consisting of two spaces. Then usefind_first_not_of
to find the end of sequence, and callerase
to remove unneeded sequences. If you do not use STL, you can use library functions:fgets
to get strings,strstr
orstrchr
to find spaces, and so on. If you need to do this manually, you can open the file in Notepad and use Replace commandantonaras wrote:
i'm going to loose all spaces i need to leave only one.
The first parameter of
Replace
contains two spaces, and the second one -- a single space, so any sequence of more then one spaces will be replaced with only one (maybe not so fast as possible). For convenience, I modified the sample using underscores. -
u can use strtok function to remove or find special character in a given string. using simlple logic, u can make it suit for ur purpose. strtok, wcstok, _mbstok[^] SaRath.
"Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++Hey Sarath thanks for the reply can i ask u a couple of questions on strtok? the link u gave me has an example of using strtok char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; can i use a string instead of char string[]? also can i put any number of deliminars in seps? thanks a lot for the help
-
If you do not use MFC, you can use STL. You can read the file using a stream object like
ifstream
, store strings instring
object, and usefind
function to find a sequence consisting of two spaces. Then usefind_first_not_of
to find the end of sequence, and callerase
to remove unneeded sequences. If you do not use STL, you can use library functions:fgets
to get strings,strstr
orstrchr
to find spaces, and so on. If you need to do this manually, you can open the file in Notepad and use Replace commandantonaras wrote:
i'm going to loose all spaces i need to leave only one.
The first parameter of
Replace
contains two spaces, and the second one -- a single space, so any sequence of more then one spaces will be replaced with only one (maybe not so fast as possible). For convenience, I modified the sample using underscores. -
Hey Sarath thanks for the reply can i ask u a couple of questions on strtok? the link u gave me has an example of using strtok char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; can i use a string instead of char string[]? also can i put any number of deliminars in seps? thanks a lot for the help
did you read my replies ? :~
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
In MFC, I think you can read the file in line-by-line manner using
CStdioFile
class, and then reduce the number of spaces using multiple calls toReplace
member ofCString
class:while(s.Replace("__", "_") != 0);
Next, you can write strings to a new file. If you need to store them in the same file, then remove the old file and rename the new one. -- modified at 4:05 Monday 12th June, 2006
Viorel. wrote:
while(s.Replace("__", "_") != 0);
The loop is not required.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
Viorel. wrote:
while(s.Replace("__", "_") != 0);
The loop is not required.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
I think the loop is required. Otherwise, in case of three consecutive spaces, only first two ones will be replaced with one space, and we will obtain two spaces in the result. Therefore we have to repeat the replace operation. It is possible to avoid loop and improve performance using other approaches.
-
I think the loop is required. Otherwise, in case of three consecutive spaces, only first two ones will be replaced with one space, and we will obtain two spaces in the result. Therefore we have to repeat the replace operation. It is possible to avoid loop and improve performance using other approaches.
Viorel. wrote:
I think the loop is required.
You're right. I spoke prematurely. :-O Sorry about that.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb