Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
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"
 
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 14-Oct-12 5:14am
Edited 14-Oct-12 5:46am
v3
Comments
Wes Aday at 14-Oct-12 11:20am
   
What about it?
Sabri Mev at 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.
Wes Aday at 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?
Sabri Mev at 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.
ProgramFOX at 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!
Sergey Alexandrovich Kryukov at 14-Oct-12 21:48pm
   
Same thing: why did you decide to copy "from somewhere on the internet". "somewhere in the internet" is not really trusty source of information.
--SA
Sergey Alexandrovich Kryukov at 14-Oct-12 21:47pm
   
This is not a productive approach? Learning programming is reading on language and programming techniques and them trying to write your own code. It's not really helpful to read some code having no explanation what it is supposed to do.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Comments
Monjurul Habib at 15-Oct-12 16:41pm
   
5!
CPallini at 15-Oct-12 16:59pm
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just run it! Or you can debug the code yourself.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 480
1 Gihan Liyanage 225
2 OriginalGriff 174
3 Bhavik_Patel 170
4 PhilLenoir 155
0 Sergey Alexandrovich Kryukov 8,653
1 OriginalGriff 7,132
2 CPallini 2,598
3 Richard MacCutchan 2,060
4 Abhinav S 1,808


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100