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.
#include <iostream>
//#include <cstring>
#include <iomanip>
#include "rpstrings.h"
usingnamespace 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;
}
elseif (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(constchar *Str1[], constchar *Str2[]);
#endif
#include "rpstrings.h"
#include <cctype>
usingnamespace std;
short rpstrcmp(constchar *Str1[], constchar *Str2[])
{
short x;
if (Str1 == Str2)
return 0;
elseif (Str1 == NULL)
return -1;
elseif (Str2 == NULL)
return 1;
else {
while (tolower(*Str1) == tolower(*Str2) && *Str1 != 0 && *Str2 != 0)
{
++Str1;
++Str2;
}
if (*Str1 < *Str2)
return -1;
elseif(*Str1 < *Str2)
return 1;
elsereturn 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).
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(constchar *Str1[], constchar *Str2[]);
but it should either
short rpstrcmp(constchar *Str1, constchar *Str2);
or
short rpstrcmp(constchar Str1[], constchar 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:
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).