By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Parse in c

P: n/a
Hi,

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?
Thanks in Advance!

Aug 14 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 14 Aug 2007 10:18:32 -0700, meendar wrote:
Hi,

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?
If you mean char *c="abcsyd\"loddggg\"kjskjdfsdf"; Try this:
char *dest = malloc(strlen(c) + 1);
int state;
if dest is a null pointer:
malloc() failed, invent something;
while *c is not a null character:
if state is 0:
read a character from c and increment it;
if it is a quotation mark:
set state to 1;
else:
add it to dest;
else:
do:
read a character from c
while it is not a quotation mark;
set state to 0;

Translating this in C is left as an exercise.
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 14 '07 #2

P: n/a
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
Aug 14 '07 #3

P: n/a
On Tue, 14 Aug 2007 10:18:32 -0700, meendar
<as****************@gmail.comwrote:
>Hi,

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?
Write down, step by step, how you would do this by looking at it and
writing the answer. Now translate that procedure into C.

--
Al Balmer
Sun City, AZ
Aug 14 '07 #4

P: n/a
On Aug 14, 11:48 pm, Al Balmer <albal...@att.netwrote:
On Tue, 14 Aug 2007 10:18:32 -0700, meendar

<askjavaprogramm...@gmail.comwrote:
Hi,
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?

Write down, step by step, how you would do this by looking at it and
writing the answer. Now translate that procedure into C.

--
Al Balmer
Sun City, AZ
Thanks to All! I found the answers very useful to me.

Aug 15 '07 #5

P: n/a
On Aug 15, 11:22 am, meendar <askjavaprogramm...@gmail.comwrote:
On Aug 14, 11:48 pm, Al Balmer <albal...@att.netwrote:


On Tue, 14 Aug 2007 10:18:32 -0700, meendar
<askjavaprogramm...@gmail.comwrote:
>Hi,
>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?
Write down, step by step, how you would do this by looking at it and
writing the answer. Now translate that procedure into C.
--
Al Balmer
Sun City, AZ

Thanks to All! I found the answers very useful to me.- Hide quoted text -

- Show quoted text -
A special thanks to Army!

Aug 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.