【问题标题】:Iterate through array of char with char goes wrong用 char 遍历 char 数组出错
【发布时间】:2017-08-31 14:52:03
【问题描述】:

我想用 char 数组中的 char 进行迭代(但我用字符串构建它)并在字符 == 迭代字符串时打印该字符。但是,第一个打印的字符与所需的字符串不同。而且它有时会在结尾添加一个意想不到的字符。

预期输出:

我是史蒂夫

输出:

~我是史蒂夫

我正在尝试让控制台使用迭代字符串打印输出,我的意思是这样的:

A

首先,控制台打印A,然后我执行回车,正如您在第 14 行看到的那样,然后转到B,直到输出是所需字符串的第一个字符。 如果输出的字符与字符相同,则将其放入数组cmem。它继续迭代第二个字符,依此类推。

代码如下:

#include <iostream>
using namespace std;
//starts here!
int main(){
    char hlw[] = "I am Steve.";
    char j; //to be iterated.
    int hlwstrlen = strlen(hlw);
    char cmem[hlwstrlen]; //memorize the correct char.
    char convertedChar; //converted char
    //iterating begin
    for (int ch = 0; ch <= hlwstrlen; ch++){
        for (int aschr = 32; aschr <= 126; aschr++){
            convertedChar = static_cast<char>(aschr); //this converts to an ascii from an int.
            cout <<  convertedChar << "\r";
            if(convertedChar == hlw[ch]){
                cout << convertedChar << "\r";
                cmem[ch] = convertedChar;

                for(int i = 0; i <= ch; i++){
                    cout << cmem[i];
                }
                continue;

             }
         }
    }
cout << endl;
return 0;
}

注意:如果我不能完美地格式化代码,我很抱歉。我用手机打字。

【问题讨论】:

    标签: c++ iteration


    【解决方案1】:
    #include <string>
    #include <iostream>
    #include <algorithm> // remove_copy_if
    #include <cctype> // isprint, isalpha, isalnum, ispunct
    
    // you check for these characters.
    // I suspect you may want std::isprint, instead.
    bool custom_exclude_filter(char c) {
        return c < 32 || c > 126;
    }
    
    int main() {
        // since you are trying to filter out bad characters,
        // let's put a bad character in the actual string
        char hlw[] = "I am \x010Steve.";
        std::string s(hlw, sizeof(hlw));
    
        // print only the printable characters
        for (auto c : s) {
            if (std::isprint(c))
                std::cout << c;
        }
        std::cout << std::endl;
    
        // works the same on hlw. The compiler knows its size already.
        for (auto c : hlw) {
            if (std::isprint(c))
                std::cout << c;
        }
        std::cout << std::endl;
    
        // same thing using your custom filter
        for (auto c : s) {
            if (!custom_exclude_filter(c))
                std::cout << c;
        }
        std::cout << std::endl;
    
        // Make a new string using only printable characters,
        // using std algorithm, and print the string
        {
            std::string temp_string;
            std::remove_copy_if(s.begin(), s.end(), std::back_inserter(temp_string), [](char c) {return 0 == std::isprint(c); });
            std::cout << temp_string << std::endl;
        }
    
        // you can iterate a string literal, using std::begin and std::end
        // see https://stackoverflow.com/a/13207440/1766544
        {
            std::string temp_string;
            std::remove_copy_if(std::begin(hlw), std::end(hlw), std::back_inserter(temp_string), [](char c) {return 0 == std::isprint(c); });
            std::cout << temp_string << std::endl;
        }
    
        // same thing using your custom filter
        {
            std::string temp_string;
            std::remove_copy_if(s.begin(), s.end(), std::back_inserter(temp_string), custom_exclude_filter);
            std::cout << temp_string << std::endl;
        }
    }
    

    【讨论】:

    • 忽略最后三种情况,现在我看到您回复 cmets 说您想要流式传输输出,因此不需要您使用的临时字符串。
    【解决方案2】:

    我用了之前在这里评论过的人的代码,忘记是谁了,也不知道为什么所有的cmets都被删除了。而且效果很好!感谢之前帮助过我的各位!

    #include <iostream>
    
    using namespace std;
    
    int main(){
        const char hlw[] = "I am Steve"; //Declare collection of char
        const int hlwstrlen = strlen(hlw);
        char *cmem = new char[hlwstrlen];
        char convertedChar;
        int len = 0;
    
         for (int ch = 0; ch < hlwstrlen; ch++){
    
            for (int aschr = 32; aschr <= 126; aschr++){
    
                const char convertedChar = static_cast<char>(aschr);
    
                cout << convertedChar << "\r";
    
                if (convertedChar == hlw[ch]){
    
                    cmem[len++] = convertedChar;
    
                    continue;
    
                }       
            }
        }
    cout << cmem << '\n';
    
    delete[] cmem;
    cout << endl;
    
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多