Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a new string that will consist of common letters from other two strings
    primarykey
    data
    text
    <p>I'm new to C programming. I have a task to do. User inputs two strings. What I need to do is to create a new string that will consist only from common letters of those two given strings. For example: if given:</p> <pre><code>str1 = "ABCDZ" str2 = "ADXYZ" </code></pre> <p>the new string will look like: <code>"ADZ"</code>. I can't make it work. I think there must be a better (more simple) algorithm but I have waisted too much time for this one so I want to complete it .. need your help!</p> <p>what I've done so far is this:</p> <pre><code>char* commonChars (char* str1, char* str2) { char *ptr, *qtr, *arr, *tmp, *ch1, *ch2; int counter = 1; ch1 = str1; ch2 = str2; arr = (char*) malloc ((strlen(str1)+strlen(str2)+1)*(sizeof(char))); //creating dynamic array strcpy(arr, str1); strcat(arr,str2); for (ptr = arr; ptr &lt; arr + strlen(arr); ptr++) { for (qtr = arr; qtr &lt; arr + strlen(arr); qtr++) // count for each char how many times is appears { if (*qtr == *ptr &amp;&amp; qtr != ptr) { counter++; tmp = qtr; } } if (counter &gt; 1) { for (qtr = tmp; *qtr; qtr++) //removing duplicate characters *(qtr) = *(qtr+1); } counter = 1; } sortArray(arr, strlen(arr)); // sorting the string in alphabetical order qtr = arr; for (ptr = arr; ptr &lt; arr + strlen(arr); ptr++, ch1++, ch2++) //checking if a letter appears in both strings and if at least one of them doesn't contain this letter - remove it { for (qtr = ptr; *qtr; qtr++) { if (*qtr != *ch1 || *qtr != *ch2) *qtr = *(qtr+1); } } } </code></pre> <p>Don't know how to finish this code .. i would be thankful for any suggestion!</p>
    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.
 

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