Java Algorithm -- simple string processing, java Algorithm --

Source: Internet
Author: User

Java Algorithm -- simple string processing, java Algorithm --

The questions are as follows:

String processing
In actual development work, string processing is the most common programming task.
This topic requires the program to process user input strings. The specific rules are as follows:
1. Change the first letter of each word to uppercase.
2. Separate numbers and letters with underscores (_) to make them clearer.
3. Adjust multiple spaces in the middle of a word to one space.
For example:
User input:
You and me what cpp2005program
Program output:
You And Me What Cpp_2005_program
User input:
This is a 99cat
Program output:
This Is A 99_cat
Assume that the string entered by the user contains only lowercase letters, spaces, and numbers, excluding other letters or symbols.
Each word is separated by one or more spaces.
Assume that the length of the string entered by the user cannot exceed 200 characters.

Method 1:

Simple handling of public class strings {public static void main (String [] args) {extends = new extends (System. in); String string = bytes. 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 () {// determines whether the first English character is in lowercase. if yes, perform the operation if (index = 0 & vector. 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) = '') {// process the Possible vector with multiple spaces. remove (index); index --;} else if (vector. elementAt (index-1) = ''& (vector. elementAt (index)> = 'A' & vector. elementAt (index) <= 'Z') {// determines the Character vector behind the space. set (index, (char) (vector. elementAt (index)-('A'-'A');} else if (vector. elementAt (index)> = 'A' & vector. elementAt (index) <= 'Z') & (vector. elementAt (index-1)> = '0' & vector. elementAt (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') {// determines the number vector. add (index, '_'); index ++;} for (int I = 0; I <vector. size (); I ++) {System. out. print (vector. elementAt (I);} System. out. println ();} catch (ArrayIndexOutOfBoundsException e ){}}}

Method 2: Use a regular expression to intercept a string, and then perform regular matching on the elements of each character array. The elements containing numbers are processed separately.

Public class SimpleString {// function for printing strings public static void print (String [] s) {for (int I = 0; I <s. length-1; I ++) {System. out. print (s [I] + "");} System. out. println (s [s. length-1]);} public static void main (String [] args) {scanned scan = new partition (System. in); String s = scan. nextLine (); String [] ss = s. split ("[\ s] +"); // based on the regular expression, delete one or more spaces and save the string as a character array for (int I = 0; I <ss. length; I ++) {// change the first letter of each character array to uppercase String up = ("" + ss [I]. charAt (0 )). toUpperCase (); // upper case StringBuffer sb = new StringBuffer (ss [I]); ss [I] = sb. replace (0, 1, up ). toString (); // The first letter of the string array has been written in upper case, and the modified character array is used to determine whether there is a number Matcher m = Pattern. compile ("\ d + "). matcher (ss [I]); // 0-9 appears once or multiple 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 + "_"; // Add "_" ss [I] = ss [I] Before and After the number. replace (num, num2); if (ss [I]. startsWith ("_") {// headers "_" ss [I] = ss [I]. substring (1);} if (ss [I]. endsWith ("_") {// 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.