Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is below the code that I need and have to understand what it does..Give me your ideas about it please..
str1 = "ABCD"
str2 = "ACDS"
str3 = "KLMN"

C++
int getUnique(char str1[],char str2[],char str3[],char arr[])
{
    int i,k,temp[26];

    for(i = 0 ; i<26 ; temp[i]=0 , ++i);
        for(i = 0 ; str1[i] ; ++i)
            temp[str1[i] - 'A'] = 1;

    for(i = 0 ; str2[i] ; ++i)
        temp[ str2[i] - 'A'] = 1 ;

    for(i = 0 ; str3[i] ; ++i)
        temp[str3[i] - 'A'] = 1;

    for(i = k = 0 ; i<26 ; ++i)
        if(temp[i])
            arr[k++] = i + 'A';
        
    arr[k] = '\0';

    return k;
}
Posted
Updated 14-Oct-12 5:46am
v3
Comments
[no name] 14-Oct-12 11:20am    
What about it?
[no name] 14-Oct-12 11:23am    
I dont know what It does. Its just a part of my project that I need to understand to use.
[no name] 14-Oct-12 11:28am    
You mean you copied some random bit of code from somewhere on the internet and you don't know what it does and you want someone to teach you how to write code? Or do you mean that you wrote a bunch of code and you have no idea what it is that you wrote?
[no name] 14-Oct-12 11:30am    
I copied it from somewhere on the internet. But I dont get what It does well. And its a part of my project , works correctly but I failed to understand whether what It does.
Thomas Daniels 14-Oct-12 11:35am    
It worked correctly. But if you copied from the internet for your project,
so you need to know what it does, otherwise you didn't add it to your project!

Quote:
for(i = 0 ; i<26 ; temp[i]=0 , ++i);
Assigns zero to every (26) items of temp.


Quote:
for(i = 0 ; str1[i] ; ++i)
temp[str1[i] - 'A'] = 1;

for(i = 0 ; str2[i] ; ++i)
temp[ str2[i] - 'A'] = 1 ;

for(i = 0 ; str3[i] ; ++i)
temp[str3[i] - 'A'] = 1;
Assigns one to the element of temp[i] having index correpondig to the letters of the passed strings, that is
  • "ABCD" => temp[0]=temp[1]=temp[2]=temp[3]=1
  • "ACDS" => temp[0]=temp[2]=temp[3]=temp[18]=1
  • ...



Quote:
for(i = k = 0 ; i<26 ; ++i)
if(temp[i])
arr[k++] = i + 'A';
Builds the string arr using only the letters corresponding to index of non-zero temp items. That is if temp[i] == 1 => append character[i] to arr
(e.g temp[0] == 1 => append 'A' to arr).


Quote:
arr[k] = '\0';

return k;
Terminates the arr string and returns arr length.


The overall result is a string containing, in alphabetic order just one occurrence (hence 'unique') of every character of the three passed string, that is
{"ABCD"," ACDS","KLMN"} => "ABCDKLMNS"

I hope my (brain compiled) code analysis is correct.
 
Share this answer
 
Comments
Monjurul Habib 15-Oct-12 16:41pm    
5!
CPallini 15-Oct-12 16:59pm    
Thank you.
Just run it! Or you can debug the code yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900