Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would split the string array using strtok (see the man page).</p> <p>So I would have something like this</p> <pre><code>char x[100]="Amit Hanish Mahesh Amit"; /* Preparing the result string */ size_t sz_result = sizeof(char) * (strlen(x) + 1); char* result = (char*) malloc( sz_result ); result[0] = '\0'; /* Parsing the string from one element to the other */ char* elm = strtok(x, " "); while( (elm = strtok(NULL, " ")) != NULL ) { ... </code></pre> <p>You will have each element of the string to verify if they are unique.</p> <p>Then I would use something like a hashmap (you can use the one from the glib) or I would put the read string element in a new string only if it is not already in.</p> <p>Here is an example for the second solution:</p> <pre><code> ... /* Is the element already in the result string? */ if ( strstr(result, elm) == NULL ) { /* Then we should add it */ snprintf( result, sz_result - 1, "%s %s", result, elm ); } } </code></pre> <p>In the end if you want x to be modified, you simply copy result in x:</p> <pre><code>strncpy( x, result, 99 ); </code></pre> <p>Here is a sample code (not optimised, not using the strn* primitives, etc.)</p> <pre><code>#include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; int main() { char x[100]="Amit Hanish Mahesh Amit"; /* Preparing the result string */ size_t sz_result = sizeof(char) * (strlen(x) + 1); char* result = (char*) malloc( sz_result ); result[0] = '\0'; /* Parsing the string from one element to the other */ char* elm = strtok(x, " "); if (elm != NULL) strcpy(result, elm); while( (elm = strtok(NULL, " ")) != NULL ) { /* Is the element already in the result string? */ if ( strstr(result, elm) == NULL ) { /* Then we should add it */ strcat( result, " " ); strcat( result, elm ); } } strcpy( x, result ); fprintf( stdout, "Result: %s\n", x ); } </code></pre>
    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. 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.
 

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