微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

操纵C风格的琴弦

如何解决操纵C风格的琴弦

我需要编写一个修改C字符串的函数,如下所示:

void foo(const char* input,ostream& fout);
//foo("test",std::cout) -> "test est st t "
//foo("hi_world",std::cout) -> "hi_world i_world world orld rld ld d "

函数仅打印字符串并每次删除一个字符。如果删除的字符是下划线,则该功能不会打印。

这是我到目前为止所拥有的:

void foo(const char* input,ostream& fout) {
    char * answer;

    while(*input) {
        if(*input == '_') {
            //somehow do nothing
        }
        if(input > input[0] {
            fout << *input+1
        }
        else {
            fout << *input << " ";
        }
    }

解决方法

我不得不更改答案,因为我是第一次错读问题,但是我认为这将满足您的需求。

$ ./answer 
test est st t 
hiworld iworld world world orld rld ld d 

运行此命令将产生以下输出。

pip install pyinstaller
,

您需要在循环中每次递增input才能更改字符串的起始索引。

如果第一个字符为continue;,请使用_跳过迭代。

要打印字符串,请使用fout << inputfout << *input只会打印一个字符,而不是整个字符串。

void foo(const char* input,ostream& fout) {
    for(; *input; input++) {
        if(input[0] == '_') {
            continue;
        }
        fout << input << " ";
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。