How to write a c program tat takes the string as input and convert it into bits and reverse the string
Reverse a string
Collapse
X
-
Treat the string as an array. Get the size (length)
Should be possible in one loop incrementing upto size,
and within the loop decrement a count down from size.
With both these values copy array into new string array.
Roughly
Code:back = size; for(c=0;c<size;c++) { newStr[c] = strArray[size]; back --; }
-
-
bits and not bytes?
You can do what they're doing for the first part, but then reverse each character's bits.
pseudo:Code:get value c set newValue = 0 shifts = size of c in bits. while (shifts > 0){ if c & 1 == 1 newValue++; c >>= 1; if (shifts > 1) newValue <<= 1; shifts --; }
Comment
-
example an input string consists of sinjanv and the last letter v in the string is a character this needs to be converted into bits and reverse the bits such tat it output give a new characterComment
-
If the OP wants to reverse the chars in a string then:
"abcd" becomes "dcba"
If the OP wants to reverse the bits in a string then ... it depends what he means by that.
"abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} for ASCII encoding.
does "abcd" become {0x86, 0x46, 0xC6, 0x26, 0x00}
or does "abcd" become {0x26, 0xC6, 0x46, 0x86, 0x00}
or does "abcd" become {0x00, 0x26, 0xC6, 0x46, 0x86}
I hesitate to offer detailed advice until I'm sure what the OP wants to do.Comment
-
If the OP wants to reverse the chars in a string then:
"abcd" becomes "dcba"
If the OP wants to reverse the bits in a string then ... it depends what he means by that.
"abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} for ASCII encoding.
does "abcd" become {0x86, 0x46, 0xC6, 0x26, 0x00}
or does "abcd" become {0x26, 0xC6, 0x46, 0x86, 0x00}
or does "abcd" become {0x00, 0x26, 0xC6, 0x46, 0x86}
I hesitate to offer detailed advice until I'm sure what the OP wants to do.Comment
-
If "abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} then what is the expected output after that string goes into your program?Comment
Comment