String permutation using iteration and a filter
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I'm looking to improve my skills as a developer so I recently challenged myself to design a string permutation generator without recursion. To add to the algorithm I also decided that the string should have a filter to it. An example of a filter would be "lld" where "l" is any character a-z and "d" is any digit 0-9. The filter "lld" should produce all strings aa0-zz9. Would love feedback on how to improve this algo. Thanks.
Implementation in C++
#include <iostream>
#define CHAR_SET_LEN 26
#define INT_SET_LEN 10
#define FILTER_LEN 3
int main()
std::cout << "Hello, World!" << std::endl;
char char_set[CHAR_SET_LEN] = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z';
char int_set[INT_SET_LEN] = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9';
bool doLoop = true;
int filterStack[100];
int filterLength = FILTER_LEN;
char filter[FILTER_LEN+1] = "lld";
for (int i=0; i < filterLength; i++)
filterStack[i] = 0;
while (doLoop)
char string[FILTER_LEN];
for (int a = 0; a < filterLength; a++)
if (filter[a] == 'l')
string[a] = char_set[filterStack[a]];
else
string[a] = int_set[filterStack[a]];
//
std::cout << string << std::endl;
//
for (int z = (filterLength-1); z > -1; z--)
filterStack[z] = filterStack[z] + 1;
int finalFilter = INT_SET_LEN;
if (filter[z] == 'l')
finalFilter = CHAR_SET_LEN;
if (filterStack[z] < finalFilter)
break;
else
if (z == 0 && (filterStack[z] == finalFilter))
doLoop = false;
break;
filterStack[z] = 0;
return 0;
c++ algorithm
add a comment |Â
up vote
2
down vote
favorite
I'm looking to improve my skills as a developer so I recently challenged myself to design a string permutation generator without recursion. To add to the algorithm I also decided that the string should have a filter to it. An example of a filter would be "lld" where "l" is any character a-z and "d" is any digit 0-9. The filter "lld" should produce all strings aa0-zz9. Would love feedback on how to improve this algo. Thanks.
Implementation in C++
#include <iostream>
#define CHAR_SET_LEN 26
#define INT_SET_LEN 10
#define FILTER_LEN 3
int main()
std::cout << "Hello, World!" << std::endl;
char char_set[CHAR_SET_LEN] = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z';
char int_set[INT_SET_LEN] = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9';
bool doLoop = true;
int filterStack[100];
int filterLength = FILTER_LEN;
char filter[FILTER_LEN+1] = "lld";
for (int i=0; i < filterLength; i++)
filterStack[i] = 0;
while (doLoop)
char string[FILTER_LEN];
for (int a = 0; a < filterLength; a++)
if (filter[a] == 'l')
string[a] = char_set[filterStack[a]];
else
string[a] = int_set[filterStack[a]];
//
std::cout << string << std::endl;
//
for (int z = (filterLength-1); z > -1; z--)
filterStack[z] = filterStack[z] + 1;
int finalFilter = INT_SET_LEN;
if (filter[z] == 'l')
finalFilter = CHAR_SET_LEN;
if (filterStack[z] < finalFilter)
break;
else
if (z == 0 && (filterStack[z] == finalFilter))
doLoop = false;
break;
filterStack[z] = 0;
return 0;
c++ algorithm
I think you needchar string[FILTER_LEN + 1]; string[FILTER_LEN] = ''
to create a null-terminated string. If yourstd::cout << string
works, it's by chance.
â Cris Luengo
Jan 12 at 16:27
Alternatively, create a standard string:std::string(string, sizeof string)
. You might want to rename your variable!
â Toby Speight
Jan 12 at 16:41
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm looking to improve my skills as a developer so I recently challenged myself to design a string permutation generator without recursion. To add to the algorithm I also decided that the string should have a filter to it. An example of a filter would be "lld" where "l" is any character a-z and "d" is any digit 0-9. The filter "lld" should produce all strings aa0-zz9. Would love feedback on how to improve this algo. Thanks.
Implementation in C++
#include <iostream>
#define CHAR_SET_LEN 26
#define INT_SET_LEN 10
#define FILTER_LEN 3
int main()
std::cout << "Hello, World!" << std::endl;
char char_set[CHAR_SET_LEN] = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z';
char int_set[INT_SET_LEN] = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9';
bool doLoop = true;
int filterStack[100];
int filterLength = FILTER_LEN;
char filter[FILTER_LEN+1] = "lld";
for (int i=0; i < filterLength; i++)
filterStack[i] = 0;
while (doLoop)
char string[FILTER_LEN];
for (int a = 0; a < filterLength; a++)
if (filter[a] == 'l')
string[a] = char_set[filterStack[a]];
else
string[a] = int_set[filterStack[a]];
//
std::cout << string << std::endl;
//
for (int z = (filterLength-1); z > -1; z--)
filterStack[z] = filterStack[z] + 1;
int finalFilter = INT_SET_LEN;
if (filter[z] == 'l')
finalFilter = CHAR_SET_LEN;
if (filterStack[z] < finalFilter)
break;
else
if (z == 0 && (filterStack[z] == finalFilter))
doLoop = false;
break;
filterStack[z] = 0;
return 0;
c++ algorithm
I'm looking to improve my skills as a developer so I recently challenged myself to design a string permutation generator without recursion. To add to the algorithm I also decided that the string should have a filter to it. An example of a filter would be "lld" where "l" is any character a-z and "d" is any digit 0-9. The filter "lld" should produce all strings aa0-zz9. Would love feedback on how to improve this algo. Thanks.
Implementation in C++
#include <iostream>
#define CHAR_SET_LEN 26
#define INT_SET_LEN 10
#define FILTER_LEN 3
int main()
std::cout << "Hello, World!" << std::endl;
char char_set[CHAR_SET_LEN] = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z';
char int_set[INT_SET_LEN] = '0', '1', '2', '3', '4', '5', '6', '7', '8', '9';
bool doLoop = true;
int filterStack[100];
int filterLength = FILTER_LEN;
char filter[FILTER_LEN+1] = "lld";
for (int i=0; i < filterLength; i++)
filterStack[i] = 0;
while (doLoop)
char string[FILTER_LEN];
for (int a = 0; a < filterLength; a++)
if (filter[a] == 'l')
string[a] = char_set[filterStack[a]];
else
string[a] = int_set[filterStack[a]];
//
std::cout << string << std::endl;
//
for (int z = (filterLength-1); z > -1; z--)
filterStack[z] = filterStack[z] + 1;
int finalFilter = INT_SET_LEN;
if (filter[z] == 'l')
finalFilter = CHAR_SET_LEN;
if (filterStack[z] < finalFilter)
break;
else
if (z == 0 && (filterStack[z] == finalFilter))
doLoop = false;
break;
filterStack[z] = 0;
return 0;
c++ algorithm
asked Jan 12 at 16:17
atr07
132
132
I think you needchar string[FILTER_LEN + 1]; string[FILTER_LEN] = ''
to create a null-terminated string. If yourstd::cout << string
works, it's by chance.
â Cris Luengo
Jan 12 at 16:27
Alternatively, create a standard string:std::string(string, sizeof string)
. You might want to rename your variable!
â Toby Speight
Jan 12 at 16:41
add a comment |Â
I think you needchar string[FILTER_LEN + 1]; string[FILTER_LEN] = ''
to create a null-terminated string. If yourstd::cout << string
works, it's by chance.
â Cris Luengo
Jan 12 at 16:27
Alternatively, create a standard string:std::string(string, sizeof string)
. You might want to rename your variable!
â Toby Speight
Jan 12 at 16:41
I think you need
char string[FILTER_LEN + 1]; string[FILTER_LEN] = ''
to create a null-terminated string. If your std::cout << string
works, it's by chance.â Cris Luengo
Jan 12 at 16:27
I think you need
char string[FILTER_LEN + 1]; string[FILTER_LEN] = ''
to create a null-terminated string. If your std::cout << string
works, it's by chance.â Cris Luengo
Jan 12 at 16:27
Alternatively, create a standard string:
std::string(string, sizeof string)
. You might want to rename your variable!â Toby Speight
Jan 12 at 16:41
Alternatively, create a standard string:
std::string(string, sizeof string)
. You might want to rename your variable!â Toby Speight
Jan 12 at 16:41
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Are you sure this is C++?
...because this is almost 100% C. Let's go over some things:
#define
is discouraged in modern C++, as is reliance on the C preprocessor in general. Just define your constants withconst
and (depending on the situation)constexpr
.- Don't define raw arrays. Use
std::array
for fixed size arrays,std::vector
for dynamic arrays, or any other standard container class. - Don't use
char
arrays if you mean strings. C++ offersstd::string
for general string handling andstd::stringstream
for string building. - Use standard algorithms. For example, your first for-loop could be replaced with a call to
std::fill
.
That said, there are
Other Issues
- Don't use
std::endl
.'n'
does the same thing, but doesn't include the unnecessary flushstd::endl
comes with. - You don't need
char_set
andint_set
. You can generate all lowercase letter by adding their offset in the alphabet to'a'
(i.e.'a' + 1 == 'b'
) thanks to the (almost) ubiquitous ASCII encoding. The same is true for digits. int
is not the right type for index variables. Use somethings more appropriate, such asstd::size_t
, instead.- You should export some of your code to proper functions. Just putting everything into
main
makes your code hard to read and prone to bugs. - As Cris Luengo pointed out in the comments already,
string
is not null-terminated, and thus you're causing undefined behavior when writing it tostd::cout
. filter
is one character longer than it needs to be. Since you never do something with it that would require it to be null terminated (and don't even null terminate the string, just keep an unused character at the end), it suffices to make its lengthFILTER_LEN
.
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
Well, C++17std::string_view
is preferable tostd::string
where applicable.
â Deduplicator
Jan 12 at 21:56
@Deduplicator It is, but I don't think it is applicable here (except forfilter
maybe).
â Ben Steffan
Jan 12 at 21:59
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Are you sure this is C++?
...because this is almost 100% C. Let's go over some things:
#define
is discouraged in modern C++, as is reliance on the C preprocessor in general. Just define your constants withconst
and (depending on the situation)constexpr
.- Don't define raw arrays. Use
std::array
for fixed size arrays,std::vector
for dynamic arrays, or any other standard container class. - Don't use
char
arrays if you mean strings. C++ offersstd::string
for general string handling andstd::stringstream
for string building. - Use standard algorithms. For example, your first for-loop could be replaced with a call to
std::fill
.
That said, there are
Other Issues
- Don't use
std::endl
.'n'
does the same thing, but doesn't include the unnecessary flushstd::endl
comes with. - You don't need
char_set
andint_set
. You can generate all lowercase letter by adding their offset in the alphabet to'a'
(i.e.'a' + 1 == 'b'
) thanks to the (almost) ubiquitous ASCII encoding. The same is true for digits. int
is not the right type for index variables. Use somethings more appropriate, such asstd::size_t
, instead.- You should export some of your code to proper functions. Just putting everything into
main
makes your code hard to read and prone to bugs. - As Cris Luengo pointed out in the comments already,
string
is not null-terminated, and thus you're causing undefined behavior when writing it tostd::cout
. filter
is one character longer than it needs to be. Since you never do something with it that would require it to be null terminated (and don't even null terminate the string, just keep an unused character at the end), it suffices to make its lengthFILTER_LEN
.
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
Well, C++17std::string_view
is preferable tostd::string
where applicable.
â Deduplicator
Jan 12 at 21:56
@Deduplicator It is, but I don't think it is applicable here (except forfilter
maybe).
â Ben Steffan
Jan 12 at 21:59
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
add a comment |Â
up vote
1
down vote
accepted
Are you sure this is C++?
...because this is almost 100% C. Let's go over some things:
#define
is discouraged in modern C++, as is reliance on the C preprocessor in general. Just define your constants withconst
and (depending on the situation)constexpr
.- Don't define raw arrays. Use
std::array
for fixed size arrays,std::vector
for dynamic arrays, or any other standard container class. - Don't use
char
arrays if you mean strings. C++ offersstd::string
for general string handling andstd::stringstream
for string building. - Use standard algorithms. For example, your first for-loop could be replaced with a call to
std::fill
.
That said, there are
Other Issues
- Don't use
std::endl
.'n'
does the same thing, but doesn't include the unnecessary flushstd::endl
comes with. - You don't need
char_set
andint_set
. You can generate all lowercase letter by adding their offset in the alphabet to'a'
(i.e.'a' + 1 == 'b'
) thanks to the (almost) ubiquitous ASCII encoding. The same is true for digits. int
is not the right type for index variables. Use somethings more appropriate, such asstd::size_t
, instead.- You should export some of your code to proper functions. Just putting everything into
main
makes your code hard to read and prone to bugs. - As Cris Luengo pointed out in the comments already,
string
is not null-terminated, and thus you're causing undefined behavior when writing it tostd::cout
. filter
is one character longer than it needs to be. Since you never do something with it that would require it to be null terminated (and don't even null terminate the string, just keep an unused character at the end), it suffices to make its lengthFILTER_LEN
.
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
Well, C++17std::string_view
is preferable tostd::string
where applicable.
â Deduplicator
Jan 12 at 21:56
@Deduplicator It is, but I don't think it is applicable here (except forfilter
maybe).
â Ben Steffan
Jan 12 at 21:59
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Are you sure this is C++?
...because this is almost 100% C. Let's go over some things:
#define
is discouraged in modern C++, as is reliance on the C preprocessor in general. Just define your constants withconst
and (depending on the situation)constexpr
.- Don't define raw arrays. Use
std::array
for fixed size arrays,std::vector
for dynamic arrays, or any other standard container class. - Don't use
char
arrays if you mean strings. C++ offersstd::string
for general string handling andstd::stringstream
for string building. - Use standard algorithms. For example, your first for-loop could be replaced with a call to
std::fill
.
That said, there are
Other Issues
- Don't use
std::endl
.'n'
does the same thing, but doesn't include the unnecessary flushstd::endl
comes with. - You don't need
char_set
andint_set
. You can generate all lowercase letter by adding their offset in the alphabet to'a'
(i.e.'a' + 1 == 'b'
) thanks to the (almost) ubiquitous ASCII encoding. The same is true for digits. int
is not the right type for index variables. Use somethings more appropriate, such asstd::size_t
, instead.- You should export some of your code to proper functions. Just putting everything into
main
makes your code hard to read and prone to bugs. - As Cris Luengo pointed out in the comments already,
string
is not null-terminated, and thus you're causing undefined behavior when writing it tostd::cout
. filter
is one character longer than it needs to be. Since you never do something with it that would require it to be null terminated (and don't even null terminate the string, just keep an unused character at the end), it suffices to make its lengthFILTER_LEN
.
Are you sure this is C++?
...because this is almost 100% C. Let's go over some things:
#define
is discouraged in modern C++, as is reliance on the C preprocessor in general. Just define your constants withconst
and (depending on the situation)constexpr
.- Don't define raw arrays. Use
std::array
for fixed size arrays,std::vector
for dynamic arrays, or any other standard container class. - Don't use
char
arrays if you mean strings. C++ offersstd::string
for general string handling andstd::stringstream
for string building. - Use standard algorithms. For example, your first for-loop could be replaced with a call to
std::fill
.
That said, there are
Other Issues
- Don't use
std::endl
.'n'
does the same thing, but doesn't include the unnecessary flushstd::endl
comes with. - You don't need
char_set
andint_set
. You can generate all lowercase letter by adding their offset in the alphabet to'a'
(i.e.'a' + 1 == 'b'
) thanks to the (almost) ubiquitous ASCII encoding. The same is true for digits. int
is not the right type for index variables. Use somethings more appropriate, such asstd::size_t
, instead.- You should export some of your code to proper functions. Just putting everything into
main
makes your code hard to read and prone to bugs. - As Cris Luengo pointed out in the comments already,
string
is not null-terminated, and thus you're causing undefined behavior when writing it tostd::cout
. filter
is one character longer than it needs to be. Since you never do something with it that would require it to be null terminated (and don't even null terminate the string, just keep an unused character at the end), it suffices to make its lengthFILTER_LEN
.
answered Jan 12 at 16:51
Ben Steffan
4,85011234
4,85011234
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
Well, C++17std::string_view
is preferable tostd::string
where applicable.
â Deduplicator
Jan 12 at 21:56
@Deduplicator It is, but I don't think it is applicable here (except forfilter
maybe).
â Ben Steffan
Jan 12 at 21:59
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
add a comment |Â
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
Well, C++17std::string_view
is preferable tostd::string
where applicable.
â Deduplicator
Jan 12 at 21:56
@Deduplicator It is, but I don't think it is applicable here (except forfilter
maybe).
â Ben Steffan
Jan 12 at 21:59
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
You're right. I seem to have one toe in C and one in C++. Will clean that up. Really this should be C
â atr07
Jan 12 at 21:39
Well, C++17
std::string_view
is preferable to std::string
where applicable.â Deduplicator
Jan 12 at 21:56
Well, C++17
std::string_view
is preferable to std::string
where applicable.â Deduplicator
Jan 12 at 21:56
@Deduplicator It is, but I don't think it is applicable here (except for
filter
maybe).â Ben Steffan
Jan 12 at 21:59
@Deduplicator It is, but I don't think it is applicable here (except for
filter
maybe).â Ben Steffan
Jan 12 at 21:59
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Deduplicator, due to its absence of ownership semantics, I'd prefer to use it only in template metaprogramming context, or in cases where doing otherwise will complicate things.
â Incomputable
Jan 13 at 15:36
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
@Incomputable: For best effect, use it in any and all contexts where the callee shall not acquire ownership, and preferentially in contexts where it wraps a string-literal. It's absence of ownership is its greatest strength.
â Deduplicator
Jan 13 at 16:25
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184966%2fstring-permutation-using-iteration-and-a-filter%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think you need
char string[FILTER_LEN + 1]; string[FILTER_LEN] = ''
to create a null-terminated string. If yourstd::cout << string
works, it's by chance.â Cris Luengo
Jan 12 at 16:27
Alternatively, create a standard string:
std::string(string, sizeof string)
. You might want to rename your variable!â Toby Speight
Jan 12 at 16:41