|  | 
 
 发表于 2019-11-26 19:53:44
|
显示全部楼层
   本楼为最佳答案 
| 复制代码#include <iostream>
#include <string>
#include <cctype>
void calcstr(std::string &str)
{
    int count = 0;
    for (char &i : str)
    {
        if (isdigit(i))
        {
            count++;
        }
        if (!isdigit(i))
        {
            i = 0;
        }
    }
    std::cout << "字符串中数字的个数是:" << count << "\n"
              << "字符串中的数字有:" << str << std::endl;
}
int main(int argc, char const *argv[])
{
    std::string a = "gfgf122gf5f9g7+f+g854";
    calcstr(a);
    return 0;
}
-----------------------------------------------
 E:\Users\admin\Documents\VScode\Code>cmd /C "c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-e0rmbvrj.mo0 --stdout=Microsoft-MIEngine-Out-fdgtarns.xp5 --stderr=Microsoft-MIEngine-Error-xca4qu35.qbs --pid=Microsoft-MIEngine-Pid-30jqmv5x.4bs --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi "
 源字符串:gfgf122gf5f9g7+f+g854
 字符串中数字的个数是:9
 字符串中的数字有:    122  5 9 7    854
 
 E:\Users\admin\Documents\VScode\Code>
 | 
 |