/**************************************************************
* Copyright (c) 2016, 北京郵電大學
* All rights reserved.
* 版 本 號:v1.0
* 題目描述:實現一個函數,把字符串中的每個空格都替換成“20%”.例如:輸入we are so happy ,則輸出:
* we20%are20%so20%happy.
* 輸入描述:請輸入一段字符串:
* we are so happy
* 程序輸出: we20%are20%so20%happy
* 問題分析:1.Exception in thread main java.lang.ArrayIndexOutOfBoundsException問題
* 解決方法:提示此錯誤表示字符串數組越界,即超出了字符數組的長度,仔細檢查各個字符數組的長度。
* 2.構造函數不能有返回值
* 算法描述:1.先遍歷一遍數組,統計出字符串中的空格個數,並可由此計算出替換後的字符串的長度,每替換一個空格,
* 字符串的長度都增加2,因此替換後的字符串的長度等於原來的字符串的長度再加上2*空格數目。
* 2.使用函數System.arraycopy()將原數組s複製一份到數組newArray中,函數原型是:
* public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* src:源數組; srcPos:源數組要複製的起始位置; dest:目的數組;
* destPos:目的數組放置的起始位置; length:複製的長度。
* 這樣做的原因:新生成的數組的第一個空格前的字符爲空,無法顯示,當複製一次原數組後,
* 新數組的第一個空格前即不爲空。
* 3.字符串數組中存放的是字符串,而字符數組中存放的是每一個字符。
* 完成時間:2016-03-04
***************************************************************/
package org.marsguo.offerproject;
import java.util.Scanner;
class replaceFun{
private static int count = 0;
public replaceFun(char[] s){ //構造函數
for(int i = 0;i < s.length; i++){ //判斷空格的個數
if(s[i] == ' '){
count++; //遇到空格則count+1
}
}
System.out.println("count的值爲:" + count);
int originalLenght = s.length-1; //初始長度爲s長度減1
int newLength = s.length+count*2; //新生成的數組長度
System.out.println("newLength長度爲:" + newLength);
System.out.println("originalLength長度是:"+ originalLenght);
char[] newArray = new char[newLength]; //新聲明一個數組newArray
/*使用函數System.arraycopy()將原數組s複製一份到數組newArray中,
這樣做的原因:新生成的數組的第一個空格前的字符爲空,無法顯示,當複製一次原數組後,
新數組的第一個空格前即不爲空,*/
System.arraycopy(s,0,newArray,0,s.length);
while(originalLenght >= 0&&newLength > originalLenght){ //
if(s[originalLenght] == ' '){ //判斷是否遇到空格
newArray[--newLength] = '%'; //newLength先自減1 ,後在放入newArray數組中
newArray[--newLength] = '0';
newArray[--newLength] = '2';
}else{ //如果沒有遇到空格,則將原數組的字符從後向前放入新數組的從後向前中
newArray[--newLength] = s[originalLenght];
}
originalLenght--;
}
//return newArray; 構造函數不能有返回值,不能有函數類型!!!
System.out.println("替換後的字符串爲:");
for(int i = 0 ; i<newArray.length; i ++){
System.out.print(newArray[i]);
}
}
public static void print(char[] newArray){
// System.out.println("替換後的字符串爲:");
// for(int i = 0 ; i<newArray.length; i ++){
// System.out.print(newArray[i]);
// }
}
}
/*第二種算法:
*使用java自帶的replace()函數,將一個輸入的字符串中的所有空格都直接替換爲20%;
*/
class SolutionMethod2{
public String replaceString(final String inputString) {
return inputString.replace(" ", "%20");
}
}
public class ReplaceBlank{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("請輸入一串包含空格的英文字符串:");
String str = sc.nextLine();//這裏出現問題!!!!輸入帶空格的字符串時,需要用.nextLine();而不是.next();
System.out.println("str是:"+ str);
sc.close();
//String s[] = new String[str.length()]; //此處生成新的字符串數組時,判斷字符串長度用str.length()而不是str.length
char[] c = str.toCharArray(); //將字符串轉化爲字符型數組中
//replaceFun repl = new replaceFun(c); //在生成對象時傳遞參數
new replaceFun(c); //匿名對象,直接向構造函數中傳遞參數
//replaceFun(c);
//repl.print(); //調用類中的方法不能用repl.print(replaceFun(s)),而是直接使用對象名.方法(參數)
System.out.println();
SolutionMethod2 solution2 = new SolutionMethod2();
System.out.println("算法2輸出:" + solution2.replaceString(str));
}
}
/*第二種算法:
*使用java自帶的replace()函數,將一個輸入的字符串中的所有空格都直接替換爲20%;
public static void main(final String[] args) {
String string = "We Are Happy";
System.out.println(replaceString(string));
}
public static String replaceString(final String inputString) {
return inputString.replace(" ", "%20");
}
*/
程序運行結果: