Java algorithm--simple processing of strings __ code

Source: Internet
Author: User

The topics are as follows:

Processing of strings
In actual development work, the processing of strings is the most common programming task.
This topic is to require the program to the user input string processing. The specific rules are as follows:
1. Turn the first letter of each word into uppercase.
2. Separate the number from the letter with the underscore character (_) to make it clearer
3. Adjust the number of spaces in the middle of the word to 1 spaces.
For example:
User input:
You and me what Cpp2005program
The program output:
You and Me What Cpp_2005_program
User input:
This is a 99cat
The program output:
This is A 99_cat
We assume that the user-entered string contains only lowercase letters, spaces, and numbers, excluding other letters or symbols.
Each word is separated by one or more spaces.
Suppose the user enters a string that is no more than 200 characters long.

Method One:

Simple processing of the public class string {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in);
        String string = Scanner.nextline ();
        vector<character> vector = new vector<character> ();
        for (int i = 0; i < string.length (); i++) {Vector.add (String.charat (i));
            try {int index = 0; while (Index < vector.size ()) {//To determine whether the first lowercase English character, is the case to operate if (index = = 0 && VEC
                    Tor.elementat (Index) >= ' a ' && vector.elementat (index) <= ' z ') { Replaces the element at the specified position in this Vector with the specified element vector.set
                (Index, (char) (Vector.elementat (Index)-(' A '-' a ')); 
                    else if (vector.elementat (index-1) = = ' && vector.elementat (index) = = ') {//handle possible with multiple spaces
    Vector.remove (index);                index--; else if (vector.elementat (index-1) = = ' && (vector.elementat (index) >= ' a ' && Amp
                    Vector. ElementAt (index) <= ' z ')) {//judge is the character behind the space
                Vector.set (index, (char) (Vector.elementat (Index)-(' A '-' a '));
                        else if ((Vector.elementat (index) >= ' a ' && vector. ElementAt (index) <= ' z ') && (Vector.elementat (index-1) >= ' 0 ' && vector. El
                    Ementat (index-1) <= ' 9 ')) {vector.add (index, ' _ ');
                index++; else if (Vector.elementat (index-1) >= ' a ' && vector. ElementAt (index-1) <= ' Z
                  ') && (Vector.elementat (index) >= ' 0 ' && vector              . ElementAt (Index) <= ' 9 ") {//Judging by the number Vector.add (index, ' _ ');
                index++;
            } index++;
            for (int i = 0; i < vector.size (); i++) {System.out.print (Vector.elementat (i));
        } System.out.println ();
 The catch (ArrayIndexOutOfBoundsException e) {}}}

Method Two: The main use of regular expressions to intercept the string, and then the elements of each character array for a regular match, containing a number of separate processing

public class Simplestring {//print string function public static void print (string[] s) {for (int i = 0; i < S.L Ength-1;
        i++) {System.out.print (S[i] + "");
    } System.out.println (S[s.length-1]);
        public static void Main (string[] args) {Scanner scan = new Scanner (system.in);
        String s = scan.nextline (); string[] ss = S.split ("[\\s]+");
            Deletes one or more spaces according to the regular expression, saves the string as a character array for (int i = 0; i < ss.length; i++) {//Converts the first letter of each character array to uppercase String up = ("" + ss[i].charat (0)). toUpperCase ();
            Capital StringBuffer sb = new StringBuffer (ss[i]);

            Ss[i] = sb.replace (0, 1, up). ToString ();
            The top already capitalizes the first letter of the string array, and then determines whether there is a number Matcher m = pattern.compile ("\\d+") for the changed character array. Matcher (Ss[i]);//0-9 occurrence one or more times
                while (M.find ()) {//M.group (): Returns the input subsequence matched by the previous match String num = new String (M.grouP ());
                String num2 = num; Num2 = "_" + num + "_";
                Before and after the numbers are added "_" ss[i] = ss[i].replace (num, num2);
                if (Ss[i].startswith ("_")) {//GO Head "_" ss[i] = ss[i].substring (1);
                } if (Ss[i].endswith ("_")) {//Go Tail "_" ss[i] = ss[i].substring (0, Ss[i].length ()-1);
    }} print (ss);
 }
}

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.