好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java算法之串的简单处理

题目如下:

串的处理

在实际的开发工作中,对字符串的处理是最常见的编程任务。

本题目即是要求程序对用户输入的串进行处理。具体规则如下:

1. 把每个单词的首字母变为大写。

2. 把数字与字母之间用下划线字符(_)分开,使得更清晰

3. 把单词中间有多个空格的调整为1个空格。

例如:

用户输入:

you and me what cpp2005program

则程序输出:

you and me what cpp_2005_program

用户输入:

this is a 99cat

则程序输出:

this is a 99_cat

我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。

每个单词间由1个或多个空格分隔。

假设用户输入的串长度不超过200个字符。

方法一:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

public class 串的简单处理 {

   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()) {

         //判断第一个是否为小写的英文字符,是的话进行操作

         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) == ' ' ) {

           //处理有多个空格的可能

           vector.remove(index);

           index--;

         } else if (vector.elementat(index - 1 ) == ' '

             && (vector.elementat(index) >= 'a' && vector

                 .elementat(index) <= 'z' )) {

           //判断是空格后边的字符

           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

                 .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' )) {

           //判断的是数字

           vector.add(index, '_' );

           index++;

         }

         index++;

       }

       for ( int i = 0 ; i < vector.size(); i++) {

         system.out.print(vector.elementat(i));

       }

       system.out.println();

     } catch (arrayindexoutofboundsexception e) {

     }

   }

}

方法二:主要用到正则表达式对字符串进行截取,然后对每一个字符数组的元素进行正则匹配,含有数字的单独进行处理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

public class simplestring {

   // 打印字符串的函数

   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) {

     scanner scan = new scanner(system.in);

     string s = scan.nextline();

     string[] ss = s.split( "[\\s]+" ); // 根据正则表达式,删除一个或多个空格,将字符串保存为字符数组

     for ( int i = 0 ; i < ss.length; i++) {

       // 将每一个字符数组的首字母改为大写

       string up = ( "" + ss[i].charat( 0 )).touppercase(); // 大写

       stringbuffer sb = new stringbuffer(ss[i]);

       ss[i] = sb.replace( 0 , 1 , up).tostring();

       // 上边已经把字符串数组的首字母该为大写,然后对更改后的字符数组判断是否有数字

       matcher m = pattern.compile( "\\d+" ).matcher(ss[i]); // 0-9出现一次或多次

       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 + "_" ; // 数字前后都添加"_"

         ss[i] = ss[i].replace(num, num2);

         if (ss[i].startswith( "_" )) { // 去头"_"

           ss[i] = ss[i].substring( 1 );

         }

         if (ss[i].endswith( "_" )) { // 去尾"_"

           ss[i] = ss[i].substring( 0 , ss[i].length() - 1 );

         }

       }

     }

     print(ss);

   }

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/xlgen157387/article/details/44802193

查看更多关于Java算法之串的简单处理的详细内容...

  阅读:15次