-5
\$\begingroup\$

I am supposed to find the frequency of the unique array elements of a character array via a user defined function like int *unique(char *s), which takes a pointer to a character and returns a pointer to a dynamically allocated array of integers.

There are some constraints. I cannot use additional array nor any array indexing. Only pointer arithmetic is allowed. My solution is as follows:

 int *uniqueCount(char *s)
{
int a=0,k,p;
for(k=0;*(s+k);k++)
{
    for(p=k+1;*(s+p);p++)
    {
        if(*(s+k)==*(s+p)) break;
     }
    if(*(s+p)==0) a++;
}
int *arr,i=0;
arr=malloc(sizeof(int)*a);
for(k=0;*(s+k);k++)
{
    *(arr+i)=1;
    for(p=k+1;*(s+p);p++)
    {
        if(*(s+k)!='*'&&*(s+k)==*(s+p)){
            *(s+p)='*';
            *(arr+i)+=1;
        }
     }
     if(i<a-1) i++;
     else break;
}
return arr;
}

Some problems I face with my function:

(i) How to use the function in an actual program, like receiving the returned integer pointer and printing values.

(ii) I am changing the array element into "*" in second part of function. I want to avoid it.

I know that I've made a complex function difficult to comprehend.If there is a more optimized and simple solution, it would be helpful.

Thanks...

\$\endgroup\$

closed as off-topic by pacmaninbw, πάντα ῥεῖ, dfhwze, RoToRa, Grajdeanu Alex. Sep 29 at 12:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – pacmaninbw, πάντα ῥεῖ, dfhwze, RoToRa, Grajdeanu Alex.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ This code is very difficult to review because it isn't indented properly. Is it possible for you to reformat the code? Your first problem makes the question off-topic because it lacks concrete context, and possibly because the code wasn't tested so it might be broken. \$\endgroup\$ – pacmaninbw Sep 27 at 14:05
3
\$\begingroup\$

Please improve the variable names - it's not at all obvious what a, k and p are (and conventionally, p is usually used for a small-scope pointer, not an integer).

Always check whether malloc() (or calloc(), or realloc()) returns a non-null pointer before dereferencing.

The algorithm is so opaque that I'm not going to attempt to unravel it. It's nowhere near suitable for production use.

BTW, it's misleading to claim that you don't use array indexing when there's clearly *(arr+i) in a couple of places and *(s+p) in a couple more. Just changing the syntax doesn't stop that being a cheat.

\$\endgroup\$
  • 1
    \$\begingroup\$ I think the question is off-topic so I'm not sure an answer is appropriate. \$\endgroup\$ – pacmaninbw Sep 27 at 14:06
  • 1
    \$\begingroup\$ I felt there was enough to review, but it's not a strong conviction. Anyway, an answer won't stop the question being closed, so feel free to VTC there, @pacman \$\endgroup\$ – Toby Speight Sep 27 at 14:11
  • \$\begingroup\$ FYI, great review. \$\endgroup\$ – pacmaninbw Sep 27 at 14:12
  • 2
    \$\begingroup\$ We should downvote this, but I'll make an exception codereview.meta.stackexchange.com/questions/8945/… How else could this site see its first Lifeboat :p \$\endgroup\$ – dfhwze Sep 27 at 20:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.