用连字符替换字符串中的文本
Replacing text in a string with hyphens
我正在尝试编写一个模拟 Hangman 游戏的程序。
#include <iostream>
#include <string>
#include "assn.h"
using namespace std;
int main(){
clearScreen();
cout << "Enter a word or phrase: ";
string phrase;
std::getline(std::cin, phrase);
cout << endl << "Your phrase: " << phrase << endl;
cout << endl;
}
目前我可以获取输入字符串并保留空格,但我想创建另一个字符串,其中所有字母都替换为连字符并保留空格。我已经尝试查找它,但找不到如何操作。
这是一个手动完成的例子。我保留了您的原始字符串,以便您可以在他们猜到的时候开始替换字母。我发现最好在一开始就自己做事,而不是使用算法来了解幕后发生的事情。
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a word or phrase: ";
string originalPhrase;
std::getline(std::cin, originalPhrase);
// Copy the original string
string newPhrase(originalPhrase);
int phraseSize = originalPhrase.size();
for(int i = 0; i < phraseSize; ++i)
{
// Replace each character of the string with _
newPhrase[i] = '_';
}
cout << endl << "Your phrase: " << originalPhrase << endl;
cout << endl << "Your new phrase: " << newPhrase << endl;
cout << endl;
}
您可以使用此函数 returns 您的短语字符串的连字符字符串:
std::string replacetohyphen(std::string phrase){
for(int i=0;i<(int)phrase.length();i++){
phrase[i]='-';}
return phrase;}
用法:new_phrase=replacetohyphen(phrase);
如果您也想在这个新的带连字符的字符串中保留空格,那么 for
循环中的一个简单的 if 条件就可以解决问题:
std::string replacetohyphen(std::string phrase){
for(int i=0;i<(int)phrase.length();i++){
if(phrase[i]!=' ')phrase[i]='-';}
return phrase;}
这是一个使用 algorithm
的 replace_if
的例子
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
using namespace std;
string input{"This is a test"};
string censored{input};
replace_if(censored.begin(), censored.end(), ::isalpha, '-');
cout << censored << std::endl;
}
输出:
---- -- - ----
上面对 replace_if
的调用迭代了一个容器(在本例中,是一串字符)并将字母替换为破折号,保留空格不变。
我正在尝试编写一个模拟 Hangman 游戏的程序。
#include <iostream>
#include <string>
#include "assn.h"
using namespace std;
int main(){
clearScreen();
cout << "Enter a word or phrase: ";
string phrase;
std::getline(std::cin, phrase);
cout << endl << "Your phrase: " << phrase << endl;
cout << endl;
}
目前我可以获取输入字符串并保留空格,但我想创建另一个字符串,其中所有字母都替换为连字符并保留空格。我已经尝试查找它,但找不到如何操作。
这是一个手动完成的例子。我保留了您的原始字符串,以便您可以在他们猜到的时候开始替换字母。我发现最好在一开始就自己做事,而不是使用算法来了解幕后发生的事情。
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a word or phrase: ";
string originalPhrase;
std::getline(std::cin, originalPhrase);
// Copy the original string
string newPhrase(originalPhrase);
int phraseSize = originalPhrase.size();
for(int i = 0; i < phraseSize; ++i)
{
// Replace each character of the string with _
newPhrase[i] = '_';
}
cout << endl << "Your phrase: " << originalPhrase << endl;
cout << endl << "Your new phrase: " << newPhrase << endl;
cout << endl;
}
您可以使用此函数 returns 您的短语字符串的连字符字符串:
std::string replacetohyphen(std::string phrase){
for(int i=0;i<(int)phrase.length();i++){
phrase[i]='-';}
return phrase;}
用法:new_phrase=replacetohyphen(phrase);
如果您也想在这个新的带连字符的字符串中保留空格,那么 for
循环中的一个简单的 if 条件就可以解决问题:
std::string replacetohyphen(std::string phrase){
for(int i=0;i<(int)phrase.length();i++){
if(phrase[i]!=' ')phrase[i]='-';}
return phrase;}
这是一个使用 algorithm
的 replace_if
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
using namespace std;
string input{"This is a test"};
string censored{input};
replace_if(censored.begin(), censored.end(), ::isalpha, '-');
cout << censored << std::endl;
}
输出:
---- -- - ----
上面对 replace_if
的调用迭代了一个容器(在本例中,是一串字符)并将字母替换为破折号,保留空格不变。