Re:(9corr) string




"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




.



Relevant Pages

  • Re: [Emacs] Kommentieren
    ... ;; completely up to the user to decide, what the string ... "Chars preserved of STRING. ... `CHARS-PRESERVE' must be a parentized expression, ...
    (de.comp.editoren)
  • Re: string
    ... what will be the best way and most efficient in string method to achieve ... Then advance 4 chars, and copy from +2 to the current pointer until you ... strtok will make me loose spacing ... pointer past the year part to reach the month part. ...
    (comp.lang.c)
  • Re: FASTEST way to try all strings (a until ZZZZZZZZZZZZZZZZZZZZZZZZ)
    ... > It will be a very huge table so I in my opinion. ... > When it would be used, than it should be converted to a string, however ... >> How would an array of Byte be any faster then an array of Char? ... >> array of Byte is needed, however the OP suggested Chars (A to Z, a to z ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FASTEST way to try all strings (a until ZZZZZZZZZZZZZZZZZZZZZZZZ)
    ... > It will be a very huge table so I in my opinion. ... > When it would be used, than it should be converted to a string, however ... >> How would an array of Byte be any faster then an array of Char? ... >> array of Byte is needed, however the OP suggested Chars (A to Z, a to z ...
    (microsoft.public.dotnet.general)
  • Re: speed question
    ... chars of column A and first 2 of column B. ... as it is a string contatenation. ... > I want to have smart search like iTunes or something like that. ... > Dzemal Tipura (Dzemo) - MCP ...
    (microsoft.public.dotnet.framework.adonet)