string comparison case-insensitive program, need some help!

So I have started this program for class but am stuck on what to do next. Basically you need to create a function to compare 2 strings (case-insensitive). The strings shouldn't be altered and the return value should be similar to strcmp.

This is my main file
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
#include <iostream>
//#include <cstring>
#include <iomanip>
#include "rpstrings.h"

using namespace std;

int main ()
{
   
    char Str1[20], Str2[20];
    short result;

    cout << "Please enter the first string:" << endl;
    cin >> setw(20) >> Str1;

    cout << "Please enter the second string:" << endl;
    cin >> setw(20) >> Str2;

    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;
    }

    return 0;
}


This is my header file:
1
2
3
4
5
6
#ifndef RP_STRINGS_H_INC
#define RP_STRINGS_H_INC

short rpstrcmp(const char *Str1[], const char *Str2[]);

#endif 


This is my implementation file:
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
#include "rpstrings.h"

#include <cctype>

using namespace std;

short rpstrcmp(const char *Str1[], const char *Str2[])
{
    short x;
    if (Str1 == Str2)
        return 0;
    
    else if (Str1 == NULL)
        return -1;
    
    else if (Str2 == NULL)
        return 1;
    
    else {
        
        while (tolower(*Str1) == tolower(*Str2) && *Str1 != 0 && *Str2 != 0)
        {
            ++Str1;
            ++Str2;
        }
        
        if (*Str1 < *Str2)
            return -1;
        else if(*Str1 < *Str2)
            return 1;
        else
            return 0;
    }

    // compare a and b case INsensitively
    
    return x;
}



I don't know if I labeled the files correct. what I need to do is to add 2 defaulted arguments which will allow the user to request that you skip spaces and/or skip punctuation when doing the comparison.

Also I am trying to figure of how I can sort the numbers when they aren't justified with leading 0's and if they aren't in the lead string (kinda of like the natural sort).

Any help would be great.
Basically you need to create a function to compare 2 strings (case-insensitive). The strings shouldn't be altered and the return value should be similar to strcmp.
You seem to have done that. It looks fine to me, except the declaration. You have
 
short rpstrcmp(const char *Str1[], const char *Str2[]);

but it should either
 
short rpstrcmp(const char *Str1, const char *Str2);

or
 
short rpstrcmp(const char Str1[], const char Str2[]);


what I need to do is to add 2 defaulted arguments which will allow the user to request that you skip spaces and/or skip punctuation when doing the comparison.
You'd change the definition to look something like:
 
short rpstrcmp(const char *Str1, const char *Str2, bool skipSpaces = false, bool skipPuntuation = false);


Also I am trying to figure of how I can sort the numbers when they aren't justified with leading 0's and if they aren't in the lead string (kinda of like the natural sort).
Make the comparison ignore the white space.
Last edited on
thanks, so I corrected my code but need some urgent help regarding the sorting code.

Here is what I have to do:

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...

Any help regarding how to start this code would be helpful.

Thanks
Topic archived. No new replies allowed.