将字符串s='ab34aa243dd78eww89' 处理为 '**34**243**78***89',然后对数字求和,结果为'**7**9**15***17'

s=‘ab34aa243dd78eww89‘
#s=‘ab34aa000dd78eww89‘

#方法1:

result=‘‘
for i in s:
    if i.isalpha():
        result+=‘*‘
    else:
        result+=i

print(result)
i=0
temp=0
ss=‘‘
#**34**243**78***89
while i<len(result):
    if result[i].isdigit():
        temp+=int(result[i])
    else:

        if temp!=0:
            ss+=str(temp)
        ss+=result[i]
        temp=0
    i+=1
ss+=str(temp)
print(ss)

#方法2 ,如果**之间的数字都为0  上述方法可能有问题,需要按此方法优化;

#s=‘ab34aa243dd78eww89‘

s=‘ab34aa000dd78eww89‘
ss=‘‘
temp=‘‘
for i in s:
    if i>=‘a‘and i<=‘z‘:

        if temp!=‘‘:
            ss+=str(sum(map(int,temp)))
        ss+=‘*‘
        temp=‘‘

    else:
        temp+=i

ss+=str(sum(map(int,temp)))
print(ss)

将字符串s='ab34aa243dd78eww89' 处理为 '**34**243**78***89',然后对数字求和,结果为'**7**9**15***17'

原文地址:https://www.cnblogs.com/xiaoxiao075/p/10337983.html

时间: 2024-08-26 14:10:08

将字符串s='ab34aa243dd78eww89' 处理为 '**34**243**78***89',然后对数字求和,结果为'**7**9**15***17'的相关文章

Java学习(6):输入一串字符串,分别统计出其中的中英文字母,空格,数字和其他字符的个数

1 import java.util.Scanner; 2 3 /** 4 * 输入一行字符,分别统计出其中的中英文字母,空格,数字和其他字符的个数 5 * 6 * @author xcx 7 * @time 2017年7月22日上午9:08:16 8 */ 9 public class Practice7 { 10 11 public static void main(String[] args) { 12 System.out.println("请输入一串字符串,我们将统计出其中的中英文字母

/*有数组{2,5,6,11,15,17,22,34,45},用户输入的任意整数 是否在数组中,如果不在打印没有该数字,如果在删掉该数字,并把后面的数字依 次往前挪动,最后用数字0补位。例如:用户输入22,那么数组中的数据变化为 {2,5,6,11,15,17,34,45,0}*/

void main() { int a[9]={2,5,6,11,15,17,22,34,35},i,j,temp,x,n=1; printf("请输入一个整数:"); scanf("%d",&x); for(i=0;i<9;i++) { if(x==a[i]) { for(j=i;j<8;j++) { a[j]=a[j+1]; } a[8]=0; n=2; break; } } switch(n) { case 1: printf("

有数组{2,5,6,11,15,17,22,34,45},将用户输入的任意整数放入数组正确的位 置中,保证顺序不能乱掉。例如:用户输入9,那么数组为{2,5,6,9,11,15,17,22 ,34,45}

#include <stdio.h>void main(){ int a[9]={2,5,6,11,15,17,22,34,35},b[10]={0,2,5,6,11,15,17,22,34,35},x,i,j,temp; printf("请输入一个整数:"); scanf("%d",&x); b[0]=x; for(i=0;i<10;i++) { for(j=0;j<9;j++) { if(b[j]>b[j+1]) { tem

从终端获取一个字符串,分别统计其中大写字母、小写字母、数字及其它字符的个数。

//从终端获取一个字符串,分别统计其中大写字母.小写字母.数字及其它字符的个数. #include<stdio.h> #include<stdio.h> int main(int argc,const char *argv[]) { char str[100]; char ch; int len,i; int letter = 0, number = 0, space = 0, other = 0; gets(str); for(i=0;i<strlen(str);i++)

求 1-2+3-4+5-6+7-8....M 的结果算法

1 static void Main(string[] args) 2 { 3 /** 4 * 算法题: 5 * 求 1-2+3-4+5-6+7-8....M 的结果. 6 * */ 7 8 //存储运算结果. 9 int result = 0; 10 //记号. 11 int flag = 1; 12 //循环次数. 13 for (int i = 1; i <= 10; i++) 14 { 15 //运算结果 16 result += i * flag; 17 //调整正负值. 18 fla

计算1-2+3-4+5-6+7-8+9-10+11-12........M的结果为多少?,面试题。

笔者最近的一系列面试题里比较经典的,看到结果的那一刻你有收获吗? 1.计算1-2+3-4+5-6+7-8+9-10+11-12........M的结果为多少?,面试题. #include<stdio.h> main() { int i=0,j=0,k=0,sum=0; printf("计算1-2+3-4+5-6+7-8+9-10+11-12........M的结果为多少?\n\n\n"); while(1) { i=0,j=0,k=0,sum=0; printf("

字符串中数字求和

给定一个字符串,计算字符串中数值的个数并求和.其中还包含了负号-,若紧跟负号的是一个数值,则表示这是一个负数,若后面跟着的不是数字,则不表示什么 输入:一个字符串 输出:数值个数 数值和 例子 输入:312ab-2-- -9--a 输出:3 301 #include<stdio.h>#include <stdlib.h>#include<math.h>#include<string.h>int strToint(char* str){    int sign

输入一个字符串,判断它有几个字母、几个数字、或其他的字符

public static void main(String[] args) { int count_abc=0,count_num=0,count_oth=0; //输入一串数 Scanner scan=new Scanner(System.in); String str = scan.next(); char[] chars = str.toCharArray(); //判断每个字符 for(int i = 0;i<chars.length;i++){ if((chars[i]>=65&a

写一个函数计算计算当参数为N的值:1-2+3-4+5-6+7-8+9-10+........+n

public int returnSum(int n){ int sum=0; for(int i=1;i<=n;i++){ int k=i;         if(k%2==0){                 k=-k;         }         sum+=k; } return sum; }