Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming a constant value as a word length looks like asking for trouble to me. I recommend against this solution.</p> <p>Look: The <code>strcmp</code> solution doesn't work for you because it treats the <code>const char*</code> arguments as <strong>nul-terminated strings</strong>. You want a function which does exactly the same, but treats the arguments as <strong>words</strong> - which translates to <em>"anything-not-a-letter"</em>-terminated string.</p> <p>One could define <code>strcmp</code> in a generic way as:</p> <pre><code>template&lt;typename EndPredicate&gt; int generic_strcmp(const char* s1, const char* s2) { char c1; char c2; do { c1 = *s1++; c2 = *s2++; if (EndPredicate(c1)) { return c1 - c2; } } while (c1 == c2); return c1 - c2; } </code></pre> <p>If EndPredicate is a function which returns true iff its argument is equal to <code>\0</code>, then we obtain a regular <code>strcmp</code> which compares 0-terminated strings.</p> <p>But in order to have a function which compares words, the only required change is the predicate. It's sufficient to use the inverted <code>isalpha</code> function from <code>&lt;cctype&gt;</code> header file to indicate that the string ends when a non-alphabetic character is encountered.</p> <p>So in your case, your comparator for the set would look like this:</p> <pre><code>#include &lt;cctype&gt; int wordcmp(const char* s1, const char* s2) { char c1; char c2; do { c1 = *s1++; c2 = *s2++; if (!isalpha(c1)) { return c1 - c2; } } while (c1 == c2); return c1 - c2; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload