i want to print the following sequence of latters...
A
B
C
.
.
Z
AA
AB
AC
.
.
AZ
BA
BB
BC
. . .
ZZ
. . .
ZZZ
. . .
ZZZZ
up to any given length of word.
How to do it?
i want to print the following sequence of latters...
A
B
C
.
.
Z
AA
AB
AC
.
.
AZ
BA
BB
BC
. . .
ZZ
. . .
ZZZ
. . .
ZZZZ
up to any given length of word.
How to do it?
my try…
int main()
{
vector<string> words;
char ch0[2] = { 'A', '\0' };
for (int i = 0; i < 26; i++)
{
const char ch = 'A' + i;
string str = string(1, ch0[0]);
str += string(1, ch);
words.push_back(str);
}
ofstream outfile("allwords.txt");
for (int i = 0; i < 26; i++)
{
outfile << words[i] << endl;
}
outfile.close();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
string str = R"(
A
B
C
.
.
Z
AA
AB
AC
.
.
AZ
BA
BB
BC
. . .
ZZ
. . .
ZZZ
. . .
ZZZZ
up to any given length of word.
How to do it?
)";
cout << str << endl;
return 0;
}
🙂🙂🙂🙂🙂<←The tone posse, ready for action.
I am really surprised that anyone actually needs this. Perl provides this functionality just by using operator ++ on a string of letters.
$x = "A";
print $x++."\n" while 1;
In C++ you can implement a similar operation like this:
#include <iostream>
#include <string>
#include <cstdlib>
void increment(std::string &s) {
for (auto i = s.rbegin(); i != s.rend(); ++i) {
if ('A' <= *i && *i < 'Z') {
++*i;
return;
}
if (*i == 'Z')
*i = 'A';
else {
std::cerr << "Found unexpected character '" << *i << "'\n";
std::exit(1);
}
}
s += 'A';
}
int main() {
std::string x = "A";
while (1) {
std::cout << x << '\n';
increment(x);
}
}
alvaro said:
I am really surprised that anyone actually needs this. Perl provides this functionality just by using operator ++ on a string of letters.
$x = "A"; print $x++."\n" while 1;
In C++ you can implement a similar operation like this:
#include <iostream> #include <string> #include <cstdlib> void increment(std::string &s) { for (auto i = s.rbegin(); i != s.rend(); ++i) { if ('A' <= *i && *i < 'Z') { ++*i; return; } if (*i == 'Z') *i = 'A'; else { std::cerr << "Found unexpected character '" << *i << "'\n"; std::exit(1); } } s += 'A'; } int main() { std::string x = "A"; while (1) { std::cout << x << '\n'; increment(x); } }
great man, thanks…
Here's a recursive version.
#include <iostream>
#include <string>
void PrintABC(int iDepth, std::string szAlpha)
{
if (iDepth == 0) {
std::cout << szAlpha << std::endl;
} else {
for (char c='A'; c<='Z'; ++c) {
szAlpha.push_back(c);
PrintABC(iDepth-1,szAlpha);
szAlpha.pop_back();
}
}
}
int main()
{
int iMaxDepth;
std::string szALpha;
std::cout << "Enter maximum length: ";
std::cin >> iMaxDepth;
std::cout << std::endl;
for (int iDepth = 1; iDepth<=iMaxDepth; ++iDepth) {
PrintABC(iDepth,szALpha);
std::cout << std::endl;
}
return 0;
}