Hi, I need some help with sorting my string comparison. Bascially what I have to do is this:
To have numbers sorted properly even when they aren't justified with leading 0's:
1
2
...
9
10
Instead of:
1
10
2
...
9
Also to have numbers sorted properly even if they aren't in the lead of the string:
a1
a2
...
a9
a10
b1
b2
...
Instead of:
a1
a10
a2
...
a9
b1
b10
b2
...
Note how the lead part of the string is sorted and then the numeric part ...almost independently.
I would really appriciate some help in starting this code for my particular program. I tried many ways but I can't figure it out.
Here is my code:
#ifndef RP_STRINGS_H_INC
#define RP_STRINGS_H_INC
short rpstrcmp(const char Str1[], const char Str2[], bool skipSpaces = false,
bool skipPunctuation = false);
#endif
#include "rpstrings.h"
#include <cctype>
#include <cstddef>
#include <iostream>
using namespace std;
short rpstrcmp(const char Str1[], const char Str2[], bool skipSpaces,
bool skipPunctuation)
{
short x;
if (Str1 == Str2)
x = 0;
else if (Str1 == NULL)
x = -1;
else if (Str2 == NULL)
x = 1;
else {
while ((skipSpaces && isspace(*Str1)) ||
(skipPunctuation && ispunct(*Str1)))
{
++Str1;
}
while ((skipSpaces && isspace(*Str2)) ||
(skipPunctuation && ispunct(*Str2)))
{
++Str2;
}
// compare a and b case INsensitively
while (tolower(*Str1) == tolower(*Str2) &&
*Str1 != '\0' && *Str2 != '\0')
{
//size_t s1;
//s1 = 0;
do
{
++Str1;
//s1++;
} while ((skipSpaces && isspace(*Str1)) ||
(skipPunctuation && ispunct(*Str1)));
//cerr<<"Skipped "<<s1<<" chars in first string\n";
//size_t s2;
//s2 = 0;
do
{
++Str2;
//s2++;
} while ((skipSpaces && isspace(*Str2)) ||
(skipPunctuation && ispunct(*Str2)));
//cerr<<"Skipped "<<s2<<" chars in second string\n";
}
if (tolower (*Str1) < tolower (*Str2))
x = -1;
else if(tolower (*Str1) > tolower (*Str2))
x = 1;
else
x = 0;
}
return x;
}
#include <iostream>
//#include <cstring>
#include <iomanip>
#include "rpstrings.h"
using namespace std;
int main ()
{
char Str1[200], Str2[200];
short result;
cout << "Please enter the first string:" << endl;
cin.getline(Str1, 200);
cout << "Please enter the second string:" << endl;
cin.getline(Str2, 200);
//cerr<<"\nReceived '"<<Str1<<"' and '"<<Str2<<"'\n";
cout << "Skipping nothing...\n";
result = rpstrcmp(Str1, Str2);
if (result > 0)
{
cout << "First string is greater than second string" << endl;
}
else if (result == 0)
{
cout << "First string is equal to second string" << endl;
}
else
{
cout << "First string is less than second string" << endl;
}
cout << "Skipping spaces...\n";
result = rpstrcmp(Str1, Str2, true);
if (result > 0)
{
cout << "First string is greater than second string" << endl;
}
else if (result == 0)
{
cout << "First string is equal to second string" << endl;
}
else
{
cout << "First string is less than second string" << endl;
}
cout << "Not skipping spaces and skipping punctuation...\n";
result = rpstrcmp(Str1, Str2, false, true);
if (result > 0)
{
cout << "First string is greater than second string" << endl;
}
else if (result == 0)
{
cout << "First string is equal to second string" << endl;
}
else
{
cout << "First string is less than second string" << endl;
}
cout << "Skipping spaces and punctuation...\n";
result = rpstrcmp(Str1, Str2, true, true);
if (result > 0)
{
cout << "First string is greater than second string" << endl;
}
else if (result == 0)
{
cout << "First string is equal to second string" << endl;
}
else
{
cout << "First string is less than second string" << endl;
}
return 0;
}
Thanks