我想在char数组中迭代一个char(但我用字符串构建它)并在字符==迭代字符串时打印字符。但是,第一个打印的字符与所需的字符串不同。它有时会在最后添加一个意想不到的角色。
预期产出:
我是史蒂夫
输出:
〜我是Stevef
我试图让控制台用迭代字符串打印输出,我的意思是:
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;
}
注意:如果我无法完美地格式化代码,我很抱歉。我用手机打字。
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
我之前使用过某人评论的代码,我忘记了谁,我不知道为什么所有评论都被删除了。而且效果很好!感谢所有帮助过我的人!
#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;
}