473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parse in c

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
5 2407
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
3
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
3
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be...
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
3
by: Bob Rundle | last post by:
I would like to get something like this to work... Type t = FindMyType(); // might be int, float, double, etc string s = "1233"; object v = t.Parse(s); This doesn't work of couse, Parse is...
3
by: Slonocode | last post by:
I have some textboxes bound to an access db. I wanted to format the textboxes that displayed currency and date info so I did the following: Dim WithEvents oBidAmt As Binding oBidAmt = New...
5
by: js | last post by:
I have a textbox contains text in the format of "yyyy/MM/dd hh:mm:ss". I need to parse the text using System.DateTime.Parse() function with custom format. I got an error using the following code. ...
2
by: Samuel R. Neff | last post by:
I'm using a quasi open-source project and am running into an exception in double.Parse which is effectively this: double.Parse(double.MinValue.ToString()) System.OverflowException: Value was...
3
by: Peter Duniho | last post by:
I'm sure there's a good explanation for this, but I can't figure it out. I tried using DateTime.Parse() with a custom DateTimeFormatInfo instance, in which I'd replaced the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.