Algorithm note _084: Blue Bridge Cup exercise 11-1 Implementing the STRCMP function (Java)

Source: Internet
Author: User
Tags strcmp

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.