元ネタは http://code.nanigac.com/forum/view/422 かな。
なんか流行ってるみたいなんでやってみた
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
#include <cstring>
char convert(char c, int flg)
{
return flg ? std::toupper(c) : std::tolower(c);
}
int main(int c, char** v)
{
if ( c < 2 ) {
std::cerr << "usage: " << v[0] << " <target string>\n";
return 0;
}
std::string target(v[1]);
std::vector<int> flg;
flg.resize(target.size());
for (int i = 0; i <= target.size(); ++i )
{
std::fill_n(flg.rbegin(), i, 1);
do {
std::string res;
std::transform(
target.begin(), target.end(),
flg.begin(),
std::back_inserter(res),
convert);
std::cout << res << "\n";
} while ( std::next_permutation(flg.begin(), flg.end()) );
}
}
> g++ -o upper_lower upper_lower.cpp
> upper_lower Hello
hello
hellO
helLo
heLlo
hEllo
Hello
helLO
heLlO
heLLo
hEllO
hElLo
hELlo
HellO
HelLo
HeLlo
HEllo
heLLO
hElLO
hELlO
hELLo
HelLO
HeLlO
HeLLo
HEllO
HElLo
HELlo
hELLO
HeLLO
HElLO
HELlO
HELLo
HELLO
うむ ( '-')
※ サンプルの引数と結果が違ってたので修正ー(汁





コメントする