Directory
1 Problem Description
2 Solutions
1 problem description Problem DescriptionImplement a function that compares the size of a string, that is, implement the STRCMP function. function: int mystrcmp (char *s1,char *s2) compares strings S1 and S2 in ASCII order. If the S1 is equal to S2 returns 0,S1>S2 returns 1,S1<S2 returns-1. Specifically, two strings are compared from left to right by character (by the size of the ASCII value) until a different character or a ' + ' is present (note that the value of ' + ' is 0, less than any ASCII character). such as:
"A" < "B"
"A" > "a"
"Computer" > "Compare "
"Hello" < "HelloWorld"Sample Outputdata size and conventionsstring length <100.
2 Solutions
The specific code is as follows:
ImportJava.util.Scanner; Public classMain { Public voidPrintresult (String A, String B) {intLenA =a.length (); intLenB =b.length (); if(LenA < 1 | | LenB < 1) return; Char[] Arraya =A.tochararray (); Char[] Arrayb =B.tochararray (); inti = 0, j = 0; intJudge = 10000; while(I < LenA && J <LenB) {Judge= arraya[i++]-arrayb[j++]; if(Judge! = 0) Break; } if(Judge > 0 && Judge! = 10000) {System.out.println ("1"); } Else if(Judge < 0) {System.out.println ("-1"); } Else { intTempi = LenA-i; intTEMPJ = LenB-J; if(Tempi = =TEMPJ) {System.out.println ("0"); } Else if(Tempi >TEMPJ) {System.out.println ("1"); } Else if(Tempi <TEMPJ) {System.out.println ("-1"); } } return; } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); String A=In.nextline (); String B=In.nextline (); Test.printresult (A, B); }}
Algorithm note _084: Blue Bridge Cup exercise 11-1 Implementing the STRCMP function (Java)