保留整数 (Java实现)


Problem Description

输入一个字符串str1,把其中的连续非数字的字符子串换成一个‘*’,存入字符数组str2 中,所有数字字符也必须依次存入 str2 中。输出str2。

Input

输入为一行字符串str1,其中可能包含空格。字符串长度不超过80个字符。

Output

输出处理好的字符串str2。

Sample Input

$Ts!47&*s456  a23* +B9k

Sample Output

*47*456*23*9*

Hint

 

Source

 

 1 import java.util.*;  2 
 3 public class Main {  4     public static void main(String[] args) {  5         Scanner sc = new Scanner(System.in);  6         String str1 = sc.nextLine();  7         char[] str2 = new char[81];  // 定义用于储存变形后字符串的字符数组
 8         int j = 0;  9         int flag = 1;  // 设置变量,标识是否已经输出过数字
10         for (int i = 0; i < str1.length(); i++) { 11             char ch = str1.charAt(i); 12             if (Character.isDigit(ch)){  // 调用isDigit方法,识别整数
13                 flag = 1; 14                 str2[j++] = ch; 15  } 16             else if (flag == 1){ 17                 str2[j++] = '*'; 18                 flag = 0; 19  } 20  } 21         for (int i = 0; i < j; i++){ 22             System.out.printf("%c", str2[i]); 23  } 24  } 25 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM