current position:Home>C + + how to do this program

C + + how to do this program

2022-02-02 18:26:30 CSDN Q & A

img

  Given a character string , It's made up of small letters and numbers , Please divide the numbers and sum them , Lowercase letters remain unchanged, enter   Enter a string of lowercase letters and numbers , Length not exceeding 100 Output   Split the string into multiple lines , The letters remain the same , Digital summation sample input   8bc123def356805 Sample output  abe  ef 14 5



Refer to the answer 1:
#include <iostream>#include <string>int main(){    std::string string;    std::cin >> string;    const char *p = string.c_str();    while (*p)    {        if (std::isdigit(*p))        {            int sum = 0;            while (*p && std::isdigit(*p))                sum += *p++ - '0';            std::cout << sum << std::endl;        }        else        {            while (*p && !std::isdigit(*p))                std::cout << *p++;            std::cout << std::endl;        }    }    return 0;}



Refer to the answer 2:



Refer to the answer 3:
#include<stdio.h>int main(){    char str[100] = {
   
   
   
   '0'};    int flag = 0;    int nums = 0;    int data = 0;    scanf("%s",&str);    int n = 0;    while (str[n]!='\0')    {        n++;    }    for (int i = 0; i < n; i++)    {        if (str[i]>='a' &&str[i]<='z')        {            if (flag==1)            {                printf("%d\n",nums);                nums = 0;                flag = 0;            }            printf("%c",str[i]);        }        if (str[i] >= '1' && str[i] <= '9')        {            data = str[i] - 48;            if (flag==0)            {                printf("\n");                flag = 1;            }        }        if (flag==1)        {            nums += data;        }    }    if (flag == 1)    {        printf("%d\n", nums);    }    return 0;}



Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021826283083.html

Random recommended