Note that there are some explanatory texts on larger screens.

plurals
  1. POarray of strings printing wrong values
    text
    copied!<p>I trying to make a program that calculates proper prefixes and proper suffixes , and then compare the sets , and then return the array containing values representing number of matched pairs , This could be used later in KMP algorithm . But the problem is prefixes and suffixes array give wrong values . Even after appending a new element at new index, it replaces the all value in the array with the new element . </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; int* lps(char *,int ); int main() { char *pat = "abababca"; int *ptr ; ptr = lps(pat,strlen(pat)); printf("\n***LPS***\n"); } int * lps (char *p,int s) { char *prefixes[s] ; char *suffixes[s] ; char tmp1[s] , tmp2[s]; int i , j , k , c1 , c2 , c; for (i = 0 ; i &lt; s ; i ++) { printf("\n\n*** --- Creating Prefixes and Suffixes for i = %d --- ***",i); c1 = 0 ; //create prefixes for (j = 0 ; j &lt; i; j++) { for (k =0 ; k &lt;= j; k++) { tmp1[k]=*(p+k); printf("\n *(p+%d)= %c , tmp1[%d]=%c",k,*(p+k),k,tmp1[k]); } tmp1[k]='\0'; printf("\nprefixes[0]:%s",prefixes[0]); prefixes[c1] = tmp1; //strcpy(prefixes[c1], tmp1); printf("\ncurrently added %s to prefixes at %d and prefixes[%d]= %s\n ",tmp1,c1,c1,prefixes[c1]); c1++; } //print prefixes for (k = 0; k&lt;c1; k++) { printf("\tprefixes[%d] = %s",k,prefixes[k]); } printf("\n"); //create suffixes c2 = 0; for (j = 1 ; j &lt;= i; j++) { for (k = j ; k &lt;= i; k++) { tmp2[k-j] = *((p+k)); printf("\n *(p+%d)= %c , tmp2[%d]=%c",k,*(p+k),k-j,tmp2[k-j]); } tmp2[k-j]='\0'; suffixes[c2] = tmp2 ; // strcpy(suffixes[c2], tmp2); printf("\ncurrently added %s to suffixes at %d and suffixes[%d]= %s\n",tmp2,c2,c2,suffixes[c2]); c2++; } //prinf suffixes for (k = 0; k&lt;c2; k++) { printf("\tsuffixes[%d] = %s",k,suffixes[k]); } printf("\n"); //compare the prefixes and suffixes c = 0 ; for (j = 0; j &lt; c1; j++) { for(k=0 ; k &lt; c2 ; k++) { printf("\nprefixes[%d] = %s , suffixes[%d] = %s\n ",j,prefixes[j],k,suffixes[k]); if (strcmp(prefixes[j], suffixes[k])==0) { c = c + 1 ; } } } } } </code></pre> <p>OUTPUT (Some part of OUTPUT) :-</p> <pre><code>prefixes[0] = ab prefixes[1] = ab //it should be prefixes[0] = a prefixes[1] = ab </code></pre>
 

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