1、将字符串中的空格或Tab键替换为"_",例如“ you are a good boy”转换为“ you_are_a_good_boy”
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。 //
#include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> vecString;
string a;
getline(cin, a);
int n = a.length();
string sTemp;
for (int i = 0; i < n; i++)
{
if (a[i] != '\t' && a[i] != ' ')
{
sTemp.push_back(a[i]);
}
else
{
if (!sTemp.empty())
{
vecString.push_back(sTemp);
}
sTemp.clear();
}
if (i == n-1)
{
vecString.push_back(sTemp);
}
}
string sPrint;
for (int i = 0; i < vecString.size(); i++)
{
sPrint += vecString[i];
if (i != vecString.size()-1)
{
sPrint += "_";
}
}
cout << sPrint << endl;
system("pause");
return 0;
}
|