Thx for in detail description and some insight
On Wed, Apr 18, 2012 at 5:40 PM, Gene <[email protected]> wrote:
> You can't solve a problem like this with only examples of <. A
> complete definition is necessary. For example, what do you do with
> "a1" <>? "2b"
> Report "mismatch?"
> What do you do with
> "1 abc" <>? "2 2"
> Do you report < or mismatch?
> Here is one of infinitely many complete definitions consistent with
> your examples:
> 1. Split each string into lists of maximal tokens consisting of all
> decimal digits or all letters. White space separates tokens but is
> otherwise ignored. Anything other than digits, letters, and
> whitespace is counted as end of string.
> 2. Call these lists A and B. Compare them pairwise. If Ai and Bi
> are
> both strings of letters, compare them lexically using UTF-8 order.
> If
> Ai and Bi are all digits, compare them numerically. Continue until
> you find an inequality between a pair and report this immediately,
> ignoring the rest of the string. If you find a pair with types
> (letters or digits) that don't match, or if one token list is shorter
> than the other, report "nothing." Otherwise if you run out of pairs,
> report equal.
> Here is code that is probably pretty close to this definition. Tasks
> like this are easier if you split them up into a token scanning step
> and a processing step. I've done that here.
>
> #include <stdio.h>
> #include <ctype.h>
>
> // Scanner return values.
> #define END 0
> #define DIGITS 1
> #define ALPHA 2
>
> // Find the start and end of the first token
> // beginning at *start, ignoring initial white space.
> int scan(char **start, char **end)
> {
> char *p = *start;
> while (isspace(*p)) ++p;
> if (isdigit(*p)) {
> *start = p;
> do ++p; while (isdigit(*p));
> *end = p;
> return DIGITS;
> }
> if (isalpha(*p)) {
> *start = p;
> do ++p; while (isalpha(*p));
> *end = p;
> return ALPHA;
> }
> return END;
> }
>
> // Return the non-negative value of the string
> // starting at p and ending at the char before end.
> int int_value(char *p, char *end)
> {
> int x = 0;
> while (p != end)
> x = 10 * x + (*p++ - '0');
> return x;
> }
>
> // Possible comparison values.
> #define LT -1
> #define EQ 0
> #define GT 1
> #define NOTHING 2
>
> // Compare the strings starting at xp and ending
> // one char before x_end where x is a or b.
> int string_compare(char *ap, char *a_end,
> char *bp, char *b_end)
> {
> while (ap < a_end && bp < b_end) {
> int diff = *ap++ - *bp++;
> if (diff < 0) return LT;
> if (diff > 0) return GT;
> }
> if (bp < b_end) return LT;
> if (ap < a_end) return GT;
> return EQ;
> }
>
> // Compare tokens in strings a and b.
> int compare(char *a, char *b)
> {
> char *a_end, *b_end;
>
> while (1) {
> int a_scan = scan(&a, &a_end);
> int b_scan = scan(&b, &b_end);
> if (a_scan != b_scan) return NOTHING;
> if (a_scan == END) return EQ;
> if (a_scan == DIGITS) {
> int a_val = int_value(a, a_end);
> int b_val = int_value(b, b_end);
> if (a_val < b_val) return LT;
> if (a_val > b_val) return GT;
> }
> else if (a_scan == ALPHA) {
> int cmp = string_compare(a, a_end, b, b_end);
> if (cmp != EQ) return cmp;
> }
> a = a_end;
> b = b_end;
> }
> }
>
> int main(void)
> {
> char *s[] = {
> "a5", "a11",
> "6xxx", "007asdf",
> "00042Q", "42s",
> "6 8", "006 9",
> };
> int i;
> for (i = 0; i < sizeof s / sizeof s[0]; i += 2) {
> int cmp = compare(s[i], s[i + 1]);
> printf("%s %c %s\n", s[i], "<=>?"[cmp + 1], s[i + 1]);
> }
> return 0;
> }
>
>
> On Apr 17, 11:46 pm, "abhishek" <[email protected]> wrote:
> > Hi,
> >
> > I need to compare string into following way. Can anyone provide me some
> > insight or algorithm in c++.
> >
> > For example:
> >
> > "a5" < "a11" - because 5 is less than 11
> > "6xxx < 007asdf" - because 6 < 7
> > "00042Q < 42s" - because Q < s alphabetically
> > "6 8" < "006 9" - because 8 < 9
> >
> > Thx in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.