Board index » cppbuilder » How do I do that ? Strings Question.......

How do I do that ? Strings Question.......


2005-12-28 12:10:22 AM
cppbuilder28
Hi all,
I have a given strings that include the words "Green"
and "Blue" in them, and I have to find out (and i don't know
how) if the word "Green" comes before the word "Blue" or after
it, example of 2 strings:
1. " ....Green.....Blue..... "
2. " .....Blue......Green..... "
I know the function strstr() that tells me if the words "Green"
or "Blue" exist in my string, but how do i know who comes first?
I think it should be easy, but i'm not sure.
Thanks! :-)
 
 

Re:How do I do that ? Strings Question.......

Thanks very much!
 

Re:How do I do that ? Strings Question.......

Thanks very much! great solutions!
 

{smallsort}

Re:How do I do that ? Strings Question.......

"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote:
Quote


if( Str.Pos( "Green" ) < Str.Pos( "Blue" ) )
{
// green is first.
}
else
{
// blue is first.
}
If neither green or blue exist, this logic reports blue is
first.
Quote
if( strstr( Str, "Green" ) < strstr( Str, "Blue" ) )
{
// green is first.
}
else
{
// blue is first.
}
Same here.
~ JD
 

Re:How do I do that ? Strings Question.......

Ramy wrote:
Quote
I have a given strings that include the words "Green"
and "Blue" in them, and I have to find out (and i don't know
how) if the word "Green" comes before the word "Blue" or after
it, example of 2 strings:
If you are using AnsiString:
AnsiString Str = " ....Green.....Blue..... ";
if( Str.Pos( "Green" ) < Str.Pos( "Blue" ) )
{
// green is first.
}
else
{
// blue is first.
}
If you are using strstr:
char *Str = " ....Green.....Blue..... ";
if( strstr( Str, "Green" ) < strstr( Str, "Blue" ) )
{
// green is first.
}
else
{
// blue is first.
}
HTH
Jonathan
jomitech.com/mtbcc32.shtml - Multi-threaded compilation for BCB
jomitech.com/forum - JomiTech Forums
 

Re:How do I do that ? Strings Question.......

"Ramy" < XXXX@XXXXX.COM >writes:
Quote
Hi all,

I have a given strings that include the words "Green"
and "Blue" in them, and I have to find out (and i don't know
how) if the word "Green" comes before the word "Blue" or after
it, example of 2 strings:

1. " ....Green.....Blue..... "

2. " .....Blue......Green..... "

I know the function strstr() that tells me if the words "Green"
or "Blue" exist in my string, but how do i know who comes first?

I think it should be easy, but i'm not sure.
char const * green = strstr(mystring, "Green");
char const * blue = strstr(mystring, "Blue");
if (green && blue)
{
bool green_comes_first = (green < blue);
}
else
{
// mystring doesn't contain Green and Blue
}
If you are positive that the string will always contain Blue and
Green, then you can optimize it:
char const * green = strstr(mystring, "Green");
assert(green);
char const * blue = strstr(green, "Blue");
bool green_comes_first = (blue != 0);
This second case continues searching for "Blue" from the point where
it found Green. So if the search fails (blue is null), then you know
that blue came before green. But if blue came after, then the pointer
will be non-zero. I put in the assertion to indicate you either need
to guarantee that Green exists, or you should handle the case where
green is null (and don't use it in the call to find Blue or else
you'll have undefined behavior.)
--
Chris (TeamB);
 

Re:How do I do that ? Strings Question.......

JD wrote:
Quote
If neither green or blue exist, this logic reports blue is
first.
He said that Green and Blue were in the string so I took that as a
definite.
Jonathan
jomitech.com/mtbcc32.shtml - Multi-threaded compilation for BCB
jomitech.com/forum - JomiTech Forums
 

Re:How do I do that ? Strings Question.......

On 27 Dec 2005 09:10:22 -0700, Ramy wrote:
Quote
I know the function strstr() that tells me if the words "Green"
or "Blue" exist in my string, but how do i know who comes first?
Depending on the sort of string you use there are a variety of ways to
do this.
with AnsiString, use the AnsiPos() function which will give you an int
result.
AnsiString thing1 = "..... blue .... green...";
int bluepos = thing1.AnsiPos("blue");
int greenpos = thing1.AnsiPos("green");
if (bluepos>greenpos)
{
// do something
}
with std::string use find() which will also give you an int
e.g.
std::string thing1 = "..... blue ..... green ...";
int bluepos = thing1.find("blue");
int greenpos = thing1.find("green");
if (bluepos != std::sting::npos && greenpos != std::string::npos &&
bluepos>greenpos)
{
// do something
}
for c-style -- well check out strcspn
--
liz
 

Re:How do I do that ? Strings Question.......

"Ramy" < XXXX@XXXXX.COM >wrote in message
Quote
I know the function strstr() that tells me if the words "Green"
or "Blue" exist in my string, but how do i know who comes first?
The easiest way is to just search for both words and then see which one has
a lower pointer value:
char *message = "This is Blue, this is Green, ...;
char *green = strstr(message, "Green");
char *blue = strstr(message, "Blue");
if( green )
{
if( blue )
{
if( green < blue )
// green comes first
else
// blue comes first
}
else
// green found, but not blue ...
}
else if( blue )
{
// blue found, but not green ...
}
else
// both green and blue not found ...
The only drawback to the above is that the source string is scanned multiple
times. If you remove the strstr() altogether, you can optimize the scanning
to perform a single pass:
char *message = "This is Blue, this is Green, ...;
char *ptr = message;
while( *ptr != '\0' )
{
if( *ptr == 'B' )
{
if( strncmp(ptr, "Blue", 4) == 0 )
{
// blue is first ...
break;
}
}
else if( *ptr == 'G' )
{
if( strncmp(ptr, "Green", 5) == 0 )
{
// green is first ...
break;
}
}
++ptr;
}
Gambit
 

Re:How do I do that ? Strings Question.......

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
If you are positive that the string will always contain Blue and
Green, then you can optimize it:

char const * green = strstr(mystring, "Green");
assert(green);
char const * blue = strstr(green, "Blue");

bool green_comes_first = (blue != 0);
This does not necessarily work if the string contains "Blue" twice,
once before "Green" and once after.
- Leo
 

Re:How do I do that ? Strings Question.......

Leo Siefert < XXXX@XXXXX.COM >wrote:
Quote
This does not necessarily work if the string contains "Blue" twice,
once before "Green" and once after.
In which case, a tighter specification is required.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

Re:How do I do that ? Strings Question.......

Leo Siefert < XXXX@XXXXX.COM >writes:
Quote
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:

>If you are positive that the string will always contain Blue and
>Green, then you can optimize it:
>
>char const * green = strstr(mystring, "Green");
>assert(green);
>char const * blue = strstr(green, "Blue");
>
>bool green_comes_first = (blue != 0);

This does not necessarily work if the string contains "Blue" twice,
once before "Green" and once after.
In that situation, since blue comes both before and after, reporting
either would be "correct". Just not exclusively correct.
It's an ambiguous situation, and thus a good point to mention. For
unambiguous results, one would have to make the additional guarantee
that Blue and Green both exist once and only once in the string.
--
Chris (TeamB);