473,320 Members | 1,732 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,320 software developers and data experts.

capture substring in the most efficient

hi,

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the " " ? which
is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.
Nov 14 '05 #1
15 1981
"Magix" <ma***@asia.com> wrote in message
news:40**********@news.tm.net.my...
let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the " " ?
which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.


There's no other practical way to do it. You could use sscanf() but that
will just use a loop internally.

Alex
Nov 14 '05 #2
"Alex Fraser" <me@privacy.net> wrote:
"Magix" <ma***@asia.com> wrote in message
news:40**********@news.tm.net.my...
let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the " " ?
which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.
There's no other practical way to do it.


Sure there is. You could use strchr(). You might run into problems if
the string can contain internal '"'s, but you'll have to devise a
solution for that case anyway.
You could use sscanf() but that will just use a loop internally.


So does strchr(), but the loop in such functions is likely to be better
optimised than a normal for loop.

Richard
Nov 14 '05 #3
Richard Bos wrote:
"Alex Fraser" <me@privacy.net> wrote:
"Magix" <ma***@asia.com> wrote in message
news:40**********@news.tm.net.my...
> let say I have this string
>
> +CCED: "xxxxxxxxx", 333
>
> What is the most efficient way to capture the string within the
> " " ? which is xxxxxxxxx
> xxxxxxxxx can be any string.
>
> For loop solutions is not the option.


There's no other practical way to do it.


Sure there is. You could use strchr(). You might run into problems
if the string can contain internal '"'s, but you'll have to devise
a solution for that case anyway.
...

Good starting point. Let's do it this way:

Assuming that string is correct with respect to what you told us:
it really starts with +CC... and terminates with a ,<number>

char destination[maxLenOfString+1];
/* if maxLenOfString is unknown, use malloc() instead */

char * pstart = strchr(yourString,'"'); /* points to first " */
char * pstop = strrchr(yourString,'"'); /* points to last " */

size_t len = pstop-pstart; /* contains one " still */

strncpy (destination, pstart, len-1);
destination[len] = '\0';

( Code not checked )

Bernhard
Nov 14 '05 #4
Bernhard Holzmayer wrote:
Richard Bos wrote:

"Alex Fraser" <me@privacy.net> wrote:

"Magix" <ma***@asia.com> wrote in message
news:40**********@news.tm.net.my...

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the
" " ? which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

There's no other practical way to do it.


Sure there is. You could use strchr(). You might run into problems
if the string can contain internal '"'s, but you'll have to devise
a solution for that case anyway.
...


Good starting point. Let's do it this way:

Assuming that string is correct with respect to what you told us:
it really starts with +CC... and terminates with a ,<number>

char destination[maxLenOfString+1];
/* if maxLenOfString is unknown, use malloc() instead */

char * pstart = strchr(yourString,'"'); /* points to first " */
char * pstop = strrchr(yourString,'"'); /* points to last " */

size_t len = pstop-pstart; /* contains one " still */

strncpy (destination, pstart, len-1);
destination[len] = '\0';

( Code not checked )

Bernhard


So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.

One must use some kind of iteration to find the substring
unless:
1. The source string has fixed length fields.
or 2. The string is lexicographically order so that one
can use other search algorithms.

Perhaps, the OP should state why "for" loops must be avoided.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #5
Thomas Matthews wrote:
So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.
I guessed that the OP didn't want to code explicit for loops,
because of poor performance, but that (hopefully optimized)
functions like strchr() would be acceptable.

One must use some kind of iteration to find the substring
unless:
1. The source string has fixed length fields.
or 2. The string is lexicographically order so that one
can use other search algorithms.


If the pattern, which the OP showed us, is fixed with respect
of the xxxx... part, it should be easy to just advance the start
pointer manually like pstart = &origin[9] or so.

But to find the length of the string, some sort of iterative or
recursive looping will certainly be unavoidable.

Once the length is known, and the pattern at the end,
again pstop could be found as something like
pstop=origin[lengthOfSTring-7].

I agree to you, that this always implies a sort of looping.

Depending on how the string was gathered,
there might be another chance if the OP could keep track of
the length (maybe a counter variable).
But that's implementation issue, not having to do with the
features of C.

Bernhard
Nov 14 '05 #6
"Magix" <ma***@asia.com> wrote in message news:<40**********@news.tm.net.my>...
hi,

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the " " ? which
is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


Why is a for loop not an option? If this is for work, then a solution
that works correctly and is reasonably efficient should be fine. If
this is for work, and your requirements are micro-managed to this
level so that you can't use for loops, I'd look for a new job.
Otherwise, this is homework.
Nov 14 '05 #7
On Tue, 06 Jul 2004 15:27:16 +0200, Bernhard Holzmayer
<ho****************@deadspam.com> wrote:
Thomas Matthews wrote:
So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.


I guessed that the OP didn't want to code explicit for loops,
because of poor performance, but that (hopefully optimized)
functions like strchr() would be acceptable.


My guess was that it's a homework question.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #8
Magix wrote:
hi,

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the " " ? which
is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


How about:

char buff[] = "+CCED: \"xxxxxxxxx\", 333";
char* result = 1+strchr(buff, '"');
*strrchr(result, '"')=0;
Nov 14 '05 #9
I was trying to optimize the code performance, to see there is other option
better than "for" loop.

"red floyd" <re********@yahoo.com> wrote in message
news:96************************@posting.google.com ...
"Magix" <ma***@asia.com> wrote in message

news:<40**********@news.tm.net.my>...
hi,

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the " " ? which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


Why is a for loop not an option? If this is for work, then a solution
that works correctly and is reasonably efficient should be fine. If
this is for work, and your requirements are micro-managed to this
level so that you can't use for loops, I'd look for a new job.
Otherwise, this is homework.

Nov 14 '05 #10
Thomas Matthews <Th****************************@sbcglobal.net> wrote:
So, what is the difference between "for" loops and the
strchr function? Yes, I know that it is a library function
and on some platforms can use processor specific instructions,
That _is_ the difference.
but they both use repetition (looping). The OP wanted a
solution that doesn't use "for" loops.


strchr() is not a for loop. It does not require you to provide a counter
or a running pointer, and handle its initialisation, increment, and
boundary check. Internally, it may even, and in optimised libraries
probably will, not use for but DJNZ or whatever the local equivalent is.

Richard
Nov 14 '05 #11
Magix <ma***@asia.com> spoke thus:
I was trying to optimize the code performance, to see there is other option
better than "for" loop.


A wise man once spake the Two Rules of Optimization:

1) Don't do it.
2) (experts only) Don't do it yet.

In all likelihood, your compiler is perfectly capable of producing
efficient code from a for loop. There's usually very little to gain
from using obscure tricks in your code, and a lot of readability to
lose.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #12
Peter Ammon wrote:
Magix wrote:
hi,

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the "
" ? which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


How about:

char buff[] = "+CCED: \"xxxxxxxxx\", 333";
char* result = 1+strchr(buff, '"');
*strrchr(result, '"')=0;


If it was a homework, as Alan guesses, your solution will certainly
gain highscore. Really neat. Compact. I like it.

Almost nothing left for the optimizer - poor thing.

Bernhard
Nov 14 '05 #13
Bernhard Holzmayer <ho****************@deadspam.com> wrote in message news:<11****************@holzmayer.ifr.rt>...
Peter Ammon wrote:
Magix wrote:
hi,

let say I have this string

+CCED: "xxxxxxxxx", 333

What is the most efficient way to capture the string within the "
" ? which is xxxxxxxxx
xxxxxxxxx can be any string.

For loop solutions is not the option.

Thanks.


How about:

char buff[] = "+CCED: \"xxxxxxxxx\", 333";
char* result = 1+strchr(buff, '"');
*strrchr(result, '"')=0;


If it was a homework, as Alan guesses, your solution will certainly
gain highscore. Really neat. Compact. I like it.

Almost nothing left for the optimizer - poor thing.


Ok, how about:

char buff[] = "+CCED: \"xxxxxxxxx\", 333";
*strrchr(1+strchr(buff, '"'), '"')=0;
Nov 14 '05 #14
red floyd wrote:

> How about:
>
> char buff[] = "+CCED: \"xxxxxxxxx\", 333";
> char* result = 1+strchr(buff, '"');
> *strrchr(result, '"')=0;

.... Ok, how about:

char buff[] = "+CCED: \"xxxxxxxxx\", 333";
*strrchr(1+strchr(buff, '"'), '"')=0;


That's getting awkward. And the result gets lost.
The first one was better.
I don't like too condensed code, bc. it's less readable.
Should be up to the compiler to do such concentration.

However, the former solution was a good compromise.
The latter isn't.

Bernhard
Nov 14 '05 #15
Bernhard Holzmayer <ho****************@deadspam.com> wrote in message news:<43****************@holzmayer.ifr.rt>...
red floyd wrote:
[redacted]
That's getting awkward. And the result gets lost.
The first one was better.
I don't like too condensed code, bc. it's less readable.
Should be up to the compiler to do such concentration.

However, the former solution was a good compromise.
The latter isn't.


I was just having fun. To be honest (and I posted such above), I
thought that the requirement for "no for loops" was artificial and
either indicated inept micromanagement or homework. I said that he
should look for a solution that works first, and if a for loop does
the job reasonably efficiently, what's wrong with that?

Since not using for loops would tend to obfuscate the code (though the
strchr/strrchr solution is nice), I viewed this whole thing as
academic, anyways.
Nov 14 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Generic Usenet Account | last post by:
I had a need to tokenize a string, and this is what I came up with. Is this the most efficient way of tokenizing, or is a more efficient solution possible? Thanks, Gary
9
by: lkrubner | last post by:
I've got a function, you can see it below, that is being called onmouseup in the textarea on my main form. The idea is to find a selection if possible and store that text in a global variable. I...
7
by: junky_fellow | last post by:
Can anyone suggest some efficient way to search a substring in a text file. For eg. suppose i want to search the pattern "abc" in a text file, then which algorithm should i use. I just want some...
29
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
7
by: jaylucier | last post by:
Howdy, I'm trying to break an input string into multpile pieces using a series of delimiters that start with an asterisk. Following the asterisk is a mulitple character identifier immediately...
6
by: Rob | last post by:
Hello, I'm sure this has come up before. I have need for a collection of all elements/objects in an HTML document that have any kind of an attribute (HTML or CSS) that is making use of a URL to...
53
by: yinglcs | last post by:
Hi, In java, there is an 'indexOf' function in String which does this: indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. is there...
9
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered...
3
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.