Board index » cppbuilder » How do I do that ? Strings Question.......
Ramy
![]() CBuilder Developer |
Ramy
![]() CBuilder Developer |
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! :-) |
Ramy
![]() CBuilder Developer |
2005-12-28 12:55:29 AM
Re:How do I do that ? Strings Question.......
Thanks very much!
|
Ramy
![]() CBuilder Developer |
2005-12-28 12:55:31 AM
Re:How do I do that ? Strings Question.......
Thanks very much! great solutions!
{smallsort} |
JD
![]() CBuilder Developer |
2005-12-28 12:56:48 AM
Re:How do I do that ? Strings Question.......
"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote:
Quote
Quoteif( strstr( Str, "Green" ) < strstr( Str, "Blue" ) ) |
Jonathan Benedicto
![]() CBuilder Developer |
2005-12-28 01:20:21 AM
Re:How do I do that ? Strings Question.......
Ramy wrote:
QuoteI have a given strings that include the words "Green" 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 |
Chris Uzdavinis
![]() CBuilder Developer |
2005-12-28 01:31:13 AM
Re:How do I do that ? Strings Question.......
"Ramy" < XXXX@XXXXX.COM >writes:
QuoteHi all, 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); |
Jonathan Benedicto
![]() CBuilder Developer |
2005-12-28 02:10:36 AM
Re:How do I do that ? Strings Question.......
JD wrote:
QuoteIf neither green or blue exist, this logic reports blue is Jonathan jomitech.com/mtbcc32.shtml - Multi-threaded compilation for BCB jomitech.com/forum - JomiTech Forums |
Liz Albin
![]() CBuilder Developer |
2005-12-28 03:40:53 AM
Re:How do I do that ? Strings Question.......
On 27 Dec 2005 09:10:22 -0700, Ramy wrote:
QuoteI know the function strstr() that tells me if the words "Green" 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 |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2005-12-28 05:17:58 PM
Re:How do I do that ? Strings Question.......
"Ramy" < XXXX@XXXXX.COM >wrote in message
QuoteI know the function strstr() that tells me if the words "Green" 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 |
Leo Siefert
![]() CBuilder Developer |
2006-01-04 11:14:54 PM
Re:How do I do that ? Strings Question.......
Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
QuoteIf you are positive that the string will always contain Blue and - Leo |
Alan Bellingham
![]() CBuilder Developer |
2006-01-04 11:26:01 PM
Re:How do I do that ? Strings Question.......
Leo Siefert < XXXX@XXXXX.COM >wrote:
QuoteThis does not necessarily work if the string contains "Blue" twice, -- ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK |
Chris Uzdavinis
![]() CBuilder Developer |
2006-01-05 01:07:57 AM
Re:How do I do that ? Strings Question.......
Leo Siefert < XXXX@XXXXX.COM >writes:
QuoteChris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote: 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); |