Re:(9corr) string
- From: "Mike Wahler" <mkwahler@xxxxxxxxxxxx>
- Date: Fri, 29 Sep 2006 16:19:22 GMT
"Mike Wahler" <mkwahler@xxxxxxxxxxxx> wrote in message
news:MCbTg.4310$o71.4214@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
"magix" <magix@xxxxxxxx> wrote in message
news:451d381d$1_2@xxxxxxxxxxxxxxxxx
"Tom St Denis" <tomstdenis@xxxxxxxxx> wrote in message
news:1159540109.777333.163610@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
magix wrote:
If I have this strings in my file:
0044541CR97274 3 7 20060824060624
0034541CR97275 4 7 20060826060624
0054544CR97276 5 7 20060830060624
and I want to remove the month in the timestamp for each of the string
line
, in this case "08", and print:
0044541CR97274 3 7 200624060624
0034541CR97275 4 7 200626060624
0054544CR97276 5 7 200630060624
what will be the best way and most efficient in string method to
achieve it
? strtok ?
Write two functions
char *skipnonwhite(char *s);
char *skipwhite(char *s);
then parsing this amounts to
s = skipnonwhite(s);
s = skipwhite(s);
s = skipnonwhite(s);
s = skipwhite(s);
s = skipnonwhite(s)
s = skipwhite(s); // hint, for loop...
Then advance 4 chars, and copy from +2 to the current pointer until you
hit a NUL.
Tom
Thanks Tom, but I'm not sure how to achieve your idea, but I don't want
to loose the spacing, strtok will make me loose spacing
strtok is not the right tool (unless you want to have to go back
and add in the spaces it would remove).
What Tom is saying is to use 'skipnonwhite()' and 'skipwhite()' functions
to advance a pointer past your first three fields in order to reach the
fourth, i.e. the one you want to modify. "Advance 4 chars" moves your
pointer past the year part to reach the month part. "Copy from +2 to
current" overwrites the month and following chars with the chars starting
2 chars past the month (2 chars is the lenght of the month part.
First, make sure all your strings are terminated (last char is a '\0'.
You could use 'sscanf()' to do the 'skipping' part, and 'strcpy() to
do the copying.
Use strcpy() to
copy from
here
|
to here |
| |
v v
20060830060624
the results is:
200630060624\04
There will still be a '4' left at the end, but all library
'str..()' functions will stop at the \0 and ignore the '4'
Just give the proper offsets (expressed with pointer values) to
strcpy(). E.g. for your example, strcpy(s + 4, s + 6) where 's' is
the start address of your string.
Alas, this advice isn't guaranteed to work. (The language standard
says if the source and destination strings submitted to 'strcpy()'
overlap, it's not guaranteed to work. The function that does make
this guarantee is 'memmove()'. 'memmove()' takes an additional
argument, the number of chars to copy. Make sure to include the
string terminator in this count.
Sorry for the incorrect info.
-Mike
Give it a shot, if it doesn't work, post your code, and we'll
help you figure out why.
-Mike
.
- References:
- string
- From: magix
- Re: string
- From: Tom St Denis
- Re: string
- From: magix
- Re: string
- From: Mike Wahler
- string
- Prev by Date: Re: string
- Next by Date: optimistic locking
- Previous by thread: Re: string
- Next by thread: Re: string
- Index(es):
Relevant Pages
|