Hello.
I have encountered a following problem "find all duplicate letters in a 2d char array and replace them with '@'"
So for instance if I have this :
a a
b c
The output would be
@ @
b c
So. I have come up with the following algorithm
/*Up there are standard 2d array memory allocation procedure with malloc, manual array filler and a array printer*/
char **ptr=arr; //this will point to the given element
for(i=MIN;i<row;i++)
{
for(j=MIN;j<col;j++)
{
buff=arr[i][j]; //seed the element
for(k=MIN;k<row;k++)
{
for(l=MIN;l<col;l++)
{
if(buff==arr[k][l]) //check occurrences
{
cnt++; //if any, increment
}
if(cnt>1) //if more than one
{
*letter_occurrence=arr[k][l]; //point out that element
}
else
{
continue;
}
}
}
}
}
//then the replacement procedure
for(i=MIN;i<row;i++)
{
for(j=MIN;j<col;j++)
{
if(arr[i][j]==*letter_occurrence)
{
arr[i][j]='@';
}
printf("%c\t",arr[i][j]);
}
printf("\n");
}
The problem with this is that well it replaces only the last letter occurrence
Input :
a a
b b
Output
a a
@ @
Thank you very much for any kind of input !