Reputation: 316
I have a string named cc:
char cc[49] = { "rrrrrrrrbbbwwwgggyyybbwwggyybbbwwwgggyyyoooooooo" };
I am trying to rearrange and combine it into a longer string like so (I know this doesn't work):
char Intermediary[120] = {
{ ("nn%c%c%cn", (cc[29], cc[40], cc[46])) },
{ ("nnn%c%cn", (cc[39], cc[47])) },
{ ("nnn%c%c%c", (cc[38], cc[48], cc[37])) },
{ ("nn%c%cnn", (cc[21], cc[28])) },
{ ("nnn%cn%c", (cc[27], cc[26])) },
{ ("n%c%c%cnn", (cc[1], cc[9], cc[20])) },
{ ("n%cn%cnn", (cc[2], cc[19])) },
{ ("n%cn%cn%c", (cc[3], cc[18], cc[17])) },
{ ("nn%cn%cn", (cc[30], cc[44])) },
{ ("nnnn%c%c", (cc[45], cc[36])) },
{ ("n%c%cnnn", (cc[4], cc[10])) },
{ ("n%cnnn%c", (cc[5], cc[16])) },
{ ("%cn%cn%cn", (cc[32], cc[31], cc[41])) },
{ ("%cnnn%cn", (cc[33], cc[42])) },
{ ("%cnnn%c%c", (cc[34], cc[43], cc[35])) },
{ ("%cn%cnnn", (cc[23], cc[22])) },
{ ("%cnnnn%c", (cc[24], cc[25])) },
{ ("%c%c%cnnn", (cc[12], cc[6], cc[11])) },
{ ("%c%cnnnn", (cc[13], cc[7])) },
{ ("%c%cnnn%c", (cc[14], cc[8], cc[15])) },
};
printf("%s", Intermediary);
This is producing the following output:
ooybybyyogbgoogwgwrg
As opposed to what should come out:
nnbyonnnnyonnnnyognnbynnnnnyngnrbynnnrnynnnrnyngnnbnonnnnnognrbnnnnrnnngwnbnonwnnnonwnnnogwnbnnnwnnnngwrbnnnwrnnnnwrnnng
What am I doing wrong? I've looked at various forum posts on StackOverflow, but none seem to help me out. I've rewritten the code in Python (it works), but Python to C converters produce unhelpfully long code for a C noob such as myself:(. Any form of assistance would be greatly appreciated or, if you're feeling extremely generous, post example code/functions below. Thank you in advance :).
EDIT:
Assuming Deduplicator's recommendation, my code now looks like this:
printf("nn%c%c%cn", cc[28], cc[39], cc[45]);
printf("nnn%c%cn", cc[38], cc[46]);
printf("nnn%c%c%c", cc[37], cc[47], cc[36]);
printf("nn%c%cnn", cc[20], cc[27]);
printf("nnn%cn%c", cc[26], cc[25]);
printf("n%c%c%cnn", cc[0], cc[8], cc[19]);
printf("n%cn%cnn", cc[1], cc[18]);
printf("n%cn%cn%c", cc[2], cc[17], cc[16]);
printf("nn%cn%cn", cc[29], cc[43]);
printf("nnnn%c%c", cc[44], cc[35]);
printf("n%c%cnnn", cc[3], cc[9]);
printf("n%cnnn%c", cc[4], cc[15]);
printf("%cn%cn%cn", cc[31], cc[30], cc[40]);
printf("%cnnn%cn", cc[32], cc[41]);
printf("%cnnn%c%c", cc[33], cc[42], cc[34]);
printf("%cn%cnnn", cc[22], cc[21]);
printf("%cnnnn%c", cc[23], cc[24]);
printf("%c%c%cnnn", cc[11], cc[5], cc[10]);
printf("%c%cnnnn", cc[12], cc[6]);
printf("%c%cnnn%c", cc[13], cc[7], cc[14]);
Upvotes: 1
Views: 66
Reputation: 45654
There are no magic inserts in C.
Instead, you must manually create your string, for example using functions like printf
.
sprintf
is good for formatting into buffers, fprintf
for printing to files, and there are a lot more useful variants.
Upvotes: 2