Pointer code explaination
Feb 8, 2011 at 5:32pm UTC
Hi all, I have the following code using pointer to remove duplicate letter from a user specified key and fill up the empty space with alphabets.
Eg.
key is: testing
the code will remove duplicate alphabet so key will become "tesing"
after that the code will fill the key to make it 26 characters using alphabet without any duplicate.
Eg. "tesingabcdfhjklmopqruvwxyz"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
bool prepareKey(char *key)
{
char *pKey1 = key;
char *pKey2;
char *pKey3;
char *pAlpha;
// if key argument is empty
if (*pKey1 == '\0' )
return false ;
while (*pKey1 != '\0' )
{
// contains non alphabetic character
if (!isalpha(*pKey1))
return false ;
// convert to lower case
*pKey1 = tolower(*pKey1);
// increment to next location
pKey1++;
}
// remove duplicates
pKey1 = key;
while (*pKey1 != '\0' )
{
pKey2 = key;
while (pKey2 != pKey1)
{
// duplicate letter found
if (*pKey2 == *pKey1)
break ;
pKey2++;
}
if (pKey2 != pKey1)
{
pKey2 = pKey1;
while (*pKey2 != '\0' )
{
pKey3 = pKey2++;
*pKey3 = *pKey2;
}
}
else
{
pKey1++;
}
}
// fill in the rest of the alphabet
pAlpha = alphabet;
while (*pAlpha != '\0' )
{
pKey1 = key;
while (*pKey1 != '\0' )
{
if (*pKey1 == *pAlpha)
break ;
pKey1++;
}
if (*pKey1 == '\0' )
{
*pKey1 = *pAlpha;
pKey1++;
*pKey1 = '\0' ;
}
pAlpha++;
}
return true ;
}
Can anyone give me a walkthrough of how the code do it? Thanks
Feb 9, 2011 at 11:18am UTC
help please anyone?
Topic archived. No new replies allowed.