Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this in one pass with something like:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main (void) { char str[] = "p123h12p97h62p32h"; // Input string. int found[256]; // Assumes 8-bit char. char *src, *dst; // Source and destination pointers. // Output initial value and set found flags to false. printf ("Before: [%s]\n", str); memset (found, 0, sizeof(found)); // One loop, processing each source character in string. for (src = dst = str; *src != '\0'; src++) { // If not yet found, flag it found and transfer it, else do nothing. if (!found[(unsigned int)(*src)]) { found[(unsigned int)(*src)] = 1; *dst++ = *src; } } // Insert new end of string, then output it. *dst = '\0'; printf (" After: [%s]\n", str); return 0; } </code></pre> <p>This removes duplicates in one single pass, using two pointers that advance independently through the string:</p> <pre><code>src | v p123h12p97h62p32h ^ | dst </code></pre> <p>The <code>src</code> pointer advances by one each iteration of the loop. A character is copied from <code>src</code> to <code>dst</code> and the <code>dst</code> pointer is advanced only if the character has not been seen before (using the <code>found</code> array). The output is:</p> <pre><code>Before: [p123h12p97h62p32h] After: [p123h976] </code></pre> <p>Note the <code>"assumes 8-bit char"</code> comment - this is fine where you know the character set is ASCII (or any other 8-bit character set) but it may not be portable to more exotic implementations. You just have to ensure that there are enough elements in the <code>found</code> array for all of your characters. For example, a 10-bit <code>char</code> type would need 1024 elements (<code>2<sup>10</sup> = 1024</code>).</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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