Write a function which takes two string variables (not C strings) as inputs and
ID: 3551083 • Letter: W
Question
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 firststring 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
int stringCompare(char str1[],char str2[]){ int i=0,flag=0; while(str1[i]!='' && str2[i]!=''){ if(str1[i]!=str2[i]){ flag=1; break; } i++; }if (flag==0 && str1[i]=='' && str2[i]=='') return 0; else if (flag && str1[i]>str2[i]) return 1; else return -1;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.