1.字符统计
package com.homework;//统计字符串中相同字符出现的次数public class StatisticString { public static void main(String[] args) { String str = "abcdeaaadfec53543543253@!#@!#$$@@!@#@!#!@3"; Boolean flag; int count; for (int i = 0; i < str.length(); i++) { flag = true; count=0; for (int k = 0; k < i; k++) { if (str.charAt(i) == str.charAt(k)) flag = false; } if (flag == true) { for (int j = 0; j < str.length(); j++) { if (str.charAt(i) == str.charAt(j)) { count++; } } System.out.println(str.charAt(i) + "出现" + count + "次"); } } }}
2.字符串反转
package com.homework;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.Scanner;//字符串反转,如I love java 将反转成 java love I. public class StringReverse { public void swap(char[] arr, int begin, int end) { while(begin < end) { char temp = arr[begin]; arr[begin] = arr[end]; arr[end] = temp; begin++; end--; } } public String swapWords(String str) { char[] arr = str.toCharArray(); swap(arr, 0, arr.length - 1); int begin = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] == ' ') { swap(arr, begin, i - 1); begin = i + 1; } } return new String(arr); } public static void main(String[] args) { //缺点:one前要有空格,不然“one”将反转为“eno” String str = " one two three four five"; System.out.println(new StringReverse().swapWords(str)); //****************************************************** //以上为全部程序,下面为补充 //****************************************************** //利用jdk自带方法实现以上功能 //public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str1 = sc.nextLine(); String[] sArr = str1.split(" ");//I love java Listlist = new ArrayList (); list = Arrays.asList(sArr); // for(int i=0;i
控制台输出
补充:你知道 “ 回 ” 字有四种写法吗?????????????????
回、囘、囬、囗
七种字符串反转方法
import java.util.Stack;public class StringReverse { public static String reverse1(String s) { int length = s.length(); if (length <= 1) return s; String left = s.substring(0, length / 2); String right = s.substring(length / 2, length); return reverse1(right) + reverse1(left); } public static String reverse2(String s) { int length = s.length(); String reverse = ""; for (int i = 0; i < length; i++) reverse = s.charAt(i) + reverse; return reverse; } public static String reverse3(String s) { char[] array = s.toCharArray(); String reverse = ""; for (int i = array.length - 1; i >= 0; i--) reverse += array[i]; return reverse; } public static String reverse4(String s) { return new StringBuffer(s).reverse().toString(); } public static String reverse5(String orig) { char[] s = orig.toCharArray(); int n = s.length - 1; int halfLength = n / 2; for (int i = 0; i <= halfLength; i++) { char temp = s[i]; s[i] = s[n - i]; s[n - i] = temp; } return new String(s); } public static String reverse6(String s) { char[] str = s.toCharArray(); int begin = 0; int end = s.length() - 1; while (begin < end) { str[begin] = (char) (str[begin] ^ str[end]); str[end] = (char) (str[begin] ^ str[end]); str[begin] = (char) (str[end] ^ str[begin]); begin++; end--; } return new String(str); } public static String reverse7(String s) { char[] str = s.toCharArray(); Stackstack = new Stack (); for (int i = 0; i < str.length; i++) stack.push(str[i]); String reversed = ""; for (int i = 0; i < str.length; i++) reversed += stack.pop(); return reversed; } }