Java input Multi-group string to solve the prefix of a given string

Source: Internet
Author: User
Tags stringbuffer
the prefix that solves the given string.

Input format:

Enter an indefinite number of pairs of strings, two per line, separated by a space. For example:
FileName filepath
Tom, Jack.
Output format:

Returns the maximum prefix for two strings, for example:
The common prefix is file
No Common Prefix # # input Sample:

FileName filepath
Tom Jack
Output Sample:
The common prefix is file
No common prefix
Copyright vivi_and_qiao Liwei Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.*;
Import Java.util.Map.Entry;
        public class main{public static void prefix (String s1,string s2) {stringbuffer sb=new stringbuffer (); for (int i=0;i< () s1.length () <s2.length () s1.length (): S2.length ()); i++) {if (S1.charat (i) ==s
            2.charAt (i)) sb.append (S1.charat (i));
        else break;
        } if (Sb.length () ==0) System.out.println ("No common prefix");
    Else System.out.println ("The common prefix is" +SB);
        public static void Main (String []args) throws IOException {/*stringbuffer sb1=new stringbuffer ();
        StringBuffer sb2=new StringBuffer ();
    Scanner in=new Scanner (system.in);
    String s1,s2;
        while (In.nextline ()!= "") {S1=in.next (); S2=in.next (); Sb1.append (S1); Sb2.append (S2);
    } System.out.println (Sb1.tostring () +sb2.tostring ()); StringBuffer sb1=new StringBuffer ();
    StringBuffer sb2=new StringBuffer ();
    BufferedReader reader = new BufferedReader (new InputStreamReader (system.in));
        do {String line = Reader.readline ();
        if (Line.equals ("")) {break;
        } int i;
        For (I=0;i<line.length (); i++) if (Line.charat (i) = = ") break;
    Prefix (line.substring (0, i-1), line.substring (I+1,line.length ()));
    } while (true); }
}
Related Article

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.