Most efficient way to create a string range in C?
-
I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:
a b
for input 'a' , 'zz' , output is:
a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz
What is the most efficient way to do it in C?
-
I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:
a b
for input 'a' , 'zz' , output is:
a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz
What is the most efficient way to do it in C?
There is no efficient way to do it you have to do a brute force roll of each character in each row. It's a standard odometer setup roll a column, when that is complete move the next column 1. Essentially it's just two functions roll character, roll next column a character. The roll next column will on occasion call itself because rolling one column needs to roll the next and next etc (watch an odometer on a car roll) As this is usually homework for a programming unit I will give you one of the functions, you need to do the other :-)
void RollCharacter (char* MyStr, unsigned short Pos) {
while (MyStr[Pos] <= 'z') {
printf("Permutation string = %s\r\n", MyStr); // Print the entry
MyStr[Pos]++; // Increment character
}
}//To test it
char TestStr[10] = { 0 }; // Clear an array of character space
TestStr[0] = 'c'; // Start start character . .. I choose "c"
RollCharacter(TestStr, 0); // Roll that single character which is column zeroThe column roll is fractionally harder but not much.
In vino veritas
-
I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:
a b
for input 'a' , 'zz' , output is:
a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz
What is the most efficient way to do it in C?
I should also say there is a generic form of counting used in encryption that uses sets of characters to numbers. It isn't fast to code because it requires loops with modulo and divides but it gives you a count in a set of restricted characters or characters in any order. Consider a set of characters abcABC and that was the digit order like 1,2,3,4,5,6 So counting 0 = a, 1 = b, 2 = c, 3 = A, 4 = B, 5= C, 6 = aa, 7 = ab, 8 = ac, 9 = ba etc The code to display a count sequence is done like this but I would not call it efficient :-)
static char CodeSet[6] = { 'a', 'b', 'c', 'A', 'B', 'C'};
void CovertCountToCode(char* Buf, unsigned short BufSize, unsigned long Count) {
if ((Buf) && (BufSize)) { // Sanity check on buffer and size
BOOL notComplete = TRUE; // Not completed conversion starts true
unsigned short column = BufSize - 2; // We start right column for this
memset(Buf, 0x20, BufSize); // Clear buffer to spaces
Buf[BufSize - 1] = '\0'; // Terminate string
do {
unsigned short digit = Count % _countof(CodeSet); // Modulo to set size to get "digit"
Buf[column] = CodeSet[digit]; // Transfer the character at set position
column--; // Move a column left
if (Count >= _countof(CodeSet)) { // If we will need to loop again
Count /= _countof(CodeSet); // Divid by set size
Count--; // Subtract one
} else notComplete = FALSE; // Loop is now complete
} while ((notComplete) && (column > 0)); // Loop until complete or out of buffer room
}
}void CountInCode(unsigned short MaxCount) {
char Buf[10]; // Buffer for our display text to build
unsigned short i;
for (i = 0; i < MaxCount; i++) { // For each count
CovertCountToCode(Buf, _countof(Buf), i); // Convert count to display text
printf("Code string = %s\r\n", Buf); // Print the display text
}
}// Code to display first 50 counts .. which will be ... a to abb
CountInCode(50);In vino veritas
-
I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:
a b
for input 'a' , 'zz' , output is:
a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz
What is the most efficient way to do it in C?
Efficient in terms of speed or lines of code? If the latter, create a string buffer and then manipulate it until it is greater than the end.