刪除單詞後綴

給定一個單詞,如果該單詞以er、ly或者ing後綴結尾, 則刪除該後綴(題目保證刪除後綴後的單詞長度不爲 00),否則不進行任何操作。
輸入格式:
輸入一行,包含一個單詞(單詞中間沒有空格,每個單詞最大長度爲 3232)
輸出格式:
輸出按照題目要求處理後的單詞。
輸出時每行末尾的多餘空格,不影響答案正確性
樣例輸入:
referer
樣例輸出:
refer

解題思路:
1.首先將字符串轉換爲字符串數組
2.然後將數組中倒數第一個、第二個、第三個元素分別重新賦值爲‘ ’或者‘\0’
3.重新輸出數組即可
整體來說,還是很容易實現的

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s = sc.next();
		char[] c = s.toCharArray();
		
			if(c[c.length-1]=='r' && c[c.length-2]=='e') {
				c[c.length-1]=' ';
				c[c.length-2]=' ';
			}
			
			if(c[c.length-1]=='y' && c[c.length-2]=='l') {
				c[c.length-1]=' ';
				c[c.length-2]=' ';	
			}
				
			if(c[c.length-1]=='g' && c[c.length-2]=='n' && c[c.length-3]=='i') {
				c[c.length-1]=' ';
				c[c.length-2]=' ';
				c[c.length-3]=' ';
		}
			System.out.print(c);	
	}
}

運行結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章