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

capture substring in the most efficient

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.