473,763 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parse in c

Hi,

I just want to parse a character string as below

Char * c=abcsyd"loddgg g"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 2441
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"loddgg g"kjskjdfsdf ;

I need to stripe out loddgg from c (inside ""). How can i do this in
c?
If you mean char *c="abcsyd\"lod dggg\"kjskjdfsd f"; 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.comw rote:
I just want to parse a character string as below
Char * c=abcsyd"loddgg g"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\"loddgg g\"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\"loddgg g\"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.comw rote:
>Hi,

I just want to parse a character string as below

Char * c=abcsyd"loddgg g"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.n etwrote:
On Tue, 14 Aug 2007 10:18:32 -0700, meendar

<askjavaprogram m...@gmail.comw rote:
Hi,
I just want to parse a character string as below
Char * c=abcsyd"loddgg g"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 <askjavaprogram m...@gmail.comw rote:
On Aug 14, 11:48 pm, Al Balmer <albal...@att.n etwrote:


On Tue, 14 Aug 2007 10:18:32 -0700, meendar
<askjavaprogram m...@gmail.comw rote:
>Hi,
>I just want to parse a character string as below
>Char * c=abcsyd"loddgg g"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
872
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 tokenize the comma separated values.I used strtok function reading line by line using fgets.but it gives some weird behavior.It doesnot stripout the "" fully.Could any body have sample code for the same so that it will be helfful for my...
24
3173
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 their types. I would like to somehow store a reference to parsing operations in this array (such as Int32.Parse, Double.Parse, SqlMoney.Parse, etc), so I can invoke the appropriate one without writing a long switch.
3
35099
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
24403
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 currency and others when it will be a text integer or decimal. //This bombs because of the string having an improper format. Decimal.Parse("$9,200.00") Thanks in advance! Mark
14
3688
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 of date formatting (RFC 822). I have been using a variation of RFC 1123 (just change the time zone to an offset, i.e. "-0800"). It seems to be writing okay, but it's failing to parse. I've tried changing the regional & language settings in my...
3
3245
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 not a member of Type. I have to think that this is possible. Is it?
3
3084
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 Binding("Text", Me.Ds1, "Items.BidAmt") txtBidAmt.DataBindings.Add(oBidAmt) Private Sub oBidAmt_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs) Handles oBidAmt.Format
5
5141
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. Could someone help me with the customization? Thanks. String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider...
2
3502
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 either too large or too small for a Double. at System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info)
3
21276
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 DateTimeFormatInfo.FullDateTimePattern property with my custom format string: DateTimeFormatInfo dtfi = (DateTimeFormatInfo)DateTimeFormatInfo.InvariantCulture.Clone(); dtfi.FullDateTimePattern = "dd/MMM/yyyy:HH:mm:ss zzz";
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.