C语言实验——保留整数

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

[html]  view plain  copy
  1.   
[java]  view plain  copy
  1. import java.util.*;  
  2. import java.math.*;  
  3. public class Main2 {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Scanner input = new Scanner(System.in);  
  7.         char str1[] = new char[100];  
  8.         char str2[] = new char[100];   
  9.         str1 = input.nextLine().toCharArray();//next()读到空格结束,nextline一行  
  10.         int j=0;  
  11.         for(int i=0;i<str1.length;i++)  
  12.         {  
  13.             if('0'<=str1[i]&&str1[i]<='9')  
  14.                 str2[j++] = str1[i];  
  15.             else  
  16.             {  
  17.                 if((i+1==str1.length)||('0'<=str1[i+1]&&str1[i+1]<='9'))//注意判断条件先后顺序  
  18.                     {str2[j++] = '*';}  
  19.                 else continue;  
  20.                   
  21.             }  
  22.         }  
  23.         str2[j] = '\0';  
  24.         for(int i=0;str2[i]!='\0';i++)  
  25.         System.out.print(str2[i]);  
  26.     }  
  27.        
  28. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值