Question:
String manipulation in C?
Pi_
2012-02-04 22:00:33 UTC
I have a C string of unknown length, it is itself a concatenation of other strings and looks like this:

S = ABCD

Where B is an encrypted value while C and D are hash values.

A is the only human readable thing and can take 1 of 4 possible values, and hence can have 4 different length.

Once I compared A to its 4 possible values, I have thus obtained its length.

I also know that both C and D are hash values and that they must be 20bytes long (20 chars).

How can I retrieve B without modifying S?
Four answers:
CPlusPlus Guru
2012-02-05 00:23:43 UTC
Try this, with code borrowed from Ratchetr:



#insert

#insert



char *getB(char *S,

size_t lenA /*some value you already know how to compute*/

) {

size_t lenS = strlen(S),

lenC = 20,

lenD = 20,

lenB = lenS - (lenA + lenB + lenC);

char *B=malloc(lenB+1);

strncpy(B, S+lenA, lenB);

return B;

}



Don't forget to call free(B) when you no longer need it.



@I_got_reported: The title makes it clear that he means C strings, the only strings available in C, unless you write your own.
anonymous
2012-02-05 06:14:46 UTC
You didn't say what TYPE of string you were using, be it c-string or real strings. If you are using c-strings, the array is just that, an array; you can manipulate it just like any other array, just have a function return the second value of the array.



That may work on the string class/object, but I'm not sure, you'd have to do some research on that or try it yourself. It may work, it may not.
Ratchetr
2012-02-05 06:26:51 UTC
Assuming it is a valid C string (a nul terminated array of char), then:



lenS = strlen(S);

lenA = ;

lenC = 20;

lenD = 20;



Now you just solve for lenB:



lenB = lenS - (lenA + lenB + lenC);
john
2012-02-05 06:12:56 UTC
Jesus christ, you will not find the answer to that here. Seriously, Yahoo answers is great for some stuff, but for this kind of thing take your issue to stackoverflow.com


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...