need help please: Write a function which takes two string variables (not C strin
ID: 3541044 • Letter: N
Question
need help please:
Write a function which takes two string variables (not C strings) as inputs and compares them. If they are equal, the function should return 0. If the first string is less than the second string, return a negative number. If the first string is greater than the second string, return a positive number. You are not allowed to use the compare method of the string data type. You must use the comparison operators. Do not write a main function. Your function does not do any cin or cout. Make sure to properly format all your code.
Explanation / Answer
please rate - thanks
any questions ask
the main was just to test the code-you don't need it
#include<iostream>
#include <string>
using namespace std;
int compare(string,string);
int main()
{cout<<compare("aaa","bbb")<<endl;
cout<<compare("aaa","aaa")<<endl;
cout<<compare("bbb","aaa")<<endl;
system("pause");
}
int compare(string a, string b)
{int i;
for(i=0;a[i]!=''||b[i]!='';i++)
if(toupper(a[i])<toupper(b[i]))
return -1;
else if (toupper(a[i])>toupper(b[i]))
return 1;
if(a[i]==''&&b[i]=='')
return 0;
if(a[i]=='')
return -1;
else
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.