How can I ignore certain strings in my string centring function?

你说的曾经没有我的故事 提交于 2019-12-02 05:47:44

So, what you are really trying to do is count the instances of $N in your string, where N is a decimal digit. To do this, just look in the string for instances of $ using std::string::find, and then check the next character to see if it is a digit.

std::string::size_type pos = 0;
while ((pos = input.find('$', pos)) != std::string::npos) {
    if (pos + 1 == input.size()) {
        break;  //  The last character of the string is a '$'
    }
    if (std::isdigit(input[pos + 1])) {
        width += 2;
    }
    ++pos;  //  Start next search from the next char
}

In order to use std::isdigit, you need to first:

#include <cctype>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!