给定一个单词,如果该单词以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); } }运行结果: