meendar <as****************@gmail.comwrote:
I just want to parse a character string as below
Char * c=abcsyd"loddggg"kjskjdfsdf;
I need to stripe out loddgg from c (inside ""). How can i do this in
c?
First of all you should start with valid C, i.e.
char * c = "abcsyd\"loddggg\"kjskjdfsdf";
It's 'char' and not 'Char" and a string must be enclosed in
double quotes. Double quotes within the string have to be
"escaped" (to keep the compiler from taking them for the
end of the string) by a backslash.
The next point is that you assign the address of a so-called
string literal to a char pointer. This is quite fine, but
only as long as you don't intend to change that string later
on. But that seems to be what you plan to do. And in this
case you need a real array of chars, initialized with
your string. You create such an array e.g. by
char c[ ] = "abcsyd\"loddggg\"kjskjdfsdf";
While this looks rather similar to what you had above
there's an important difference: you now have an array
that can be modified while above you had a just pointer
to a string you were not allowed to change.
Now to the rest of your question. In C there is a number
of function for dealing with strings, many of them starting
with "str" (you have to include the header file <string.h>
for their declarations). The one relevant for finding the
position of the string you want to eliminate is
char * strstr( const char * haystack, const char * needle )
It searches with in the string 'haystack' for the string
'needle' (in your case "loddggg") and returns a pointer to
the first occurrence of 'needle' or NULL if 'needle can't
be found in 'haystack'. (Don't worry about the 'const'
qualifiers, they tell you that strstr() will modify
neither 'haystack' nor 'needle').
Once you have found were 'needle' is in haystack' you can
simply copy all characters after 'needle' over the place
where 'needle' still starts. You find out how long the
string 'needle' is by calling the function strlen() with
'needle' as its argument. You calculate the start of the
string after 'needle' by simply adding this value to
the pointer strstr() returned. For the copying it might
look reasonable to use the strcpy() function (which nor-
mally is used for copying strings as the name indicates),
but here you can't use it since it can only be used for
strings that don't obverlap. If you look carefully you
will see that what's following "loddggg" in your string
is longer than 'loddggg" and, since you are going to copy
over the place where "loddggg" now is, parts of the fol-
lowing string will end up in places where other parts of
it were, so the there's overlap and strcpy() can't be used.
In this case you need to use the memmove() function (also
declared in <string.h>) which allows to copy memory contents
from one place to another even if they overlap. It expects
three arguments, a pointer to the place to copy to, a poin-
ter to the place to copy from and the number of characters
to copy. You find out how much to copy by again using the
strlen() function, but this time on the string you want to
copy (i.e. everything after the "loddggg"). Please note that
strlen() doesn't count in the trailing '\0' character at the
end of the string. But you have to copy it also - otherwise
the string would not end where you want to.
Regards, Jens
--
\ Jens Thoms Toerring ___
jt@toerring.de
\__________________________
http://toerring.de