C语言实验——保留整数
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
输入一个字符串str1,把其中的连续非数字的字符子串换成一个‘*’,存入字符数组str2 中,所有数字字符也必须依次存入 str2 中。输出str2。
Input
输入为一行字符串str1,其中可能包含空格。字符串长度不超过80个字符。
Output
输出处理好的字符串str2。
Example Input
$Ts!47&*s456 a23* +B9k
Example Output
*47*456*23*9*
Hint
Author
- import java.util.*;
- import java.math.*;
- public class Main2 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- char str1[] = new char[100];
- char str2[] = new char[100];
- str1 = input.nextLine().toCharArray();//next()读到空格结束,nextline一行
- int j=0;
- for(int i=0;i<str1.length;i++)
- {
- if('0'<=str1[i]&&str1[i]<='9')
- str2[j++] = str1[i];
- else
- {
- if((i+1==str1.length)||('0'<=str1[i+1]&&str1[i+1]<='9'))//注意判断条件先后顺序
- {str2[j++] = '*';}
- else continue;
- }
- }
- str2[j] = '\0';
- for(int i=0;str2[i]!='\0';i++)
- System.out.print(str2[i]);
- }
- }