DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Sorting states by unique characters help

1. Registered User
Join Date
Mar 2007
Posts
18

## Sorting states by unique characters help

Ok so I have a program where I am supposed to load a txt file into an array and have it sort the states according to the number of unique letter that they have. I have this working almost like it is supposed to I am only having problems with two states... 'Hawaii' and 'Tennessee' now I have noticed the common factor here is the duplicate last letter. I was wondering if someone would look through this code and tell me how I can remedy this problem.

// stateArray is the array that the states are loaded in alphabetical order
// numArray is an array that is supposed to hold the number of unique
//characters by this sort

void fillNum(char stateArray[][80], int numArray[])
{
int firstVar;
char lowerArray[80];
int a;
int b;
int c = 0;
int d = 0;
for (firstVar = 0; firstVar < 50; ++firstVar)
{
lower(lowerArray, stateArray, firstVar);
for (a = 0; lowerArray[a] != '\0'; ++a)
{
for (b = a + 1; lowerArray[b] != '\0'; ++b)
{
if (isspace(lowerArray[a]))
{
c = 0;
break;
}
if (lowerArray[a] == lowerArray[b])
{
c = 0;
break;
}
else
{
c = 1;
}
}
d = d + c;
}
numArray[firstVar] = d;
d = 0;
}
}

2. Senior Member
Join Date
Dec 2003
Posts
3,366
tennessee should not break it if it were related to the last part of the string because it already has counted the 'e' earlier. The bug might be something else?

3. Registered User
Join Date
Mar 2007
Posts
18
I don't know why it made a difference but I reversed the sort and if fixed it

Code:
```void fillNum(char stateArray[][80], int numArray[])
{
int firstVar;
char lowerArray[80];
int a;
int b;
int c = 0;
int d = 0;
for (firstVar = 0; firstVar < 50; ++firstVar)
{
lower(lowerArray, stateArray, firstVar);
for (a = 1; lowerArray[a] != '\0'; ++a)
{
for (b = a - 1; lowerArray[b] != '\0'; --b)
{
if (isspace(lowerArray[a]))
{
c = 0;
break;
}
if (lowerArray[a] == lowerArray[b])
{
c = 0;
break;
}
else
{
c = 1;
}
}
d = d + c;
}
numArray[firstVar] = d;
d = 0;
}
}```
Last edited by emohs; 03-22-2007 at 11:16 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

 FAQ Latest Articles Java .NET XML Database Enterprise