468,720 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Reverse a string

How to write a c program tat takes the string as input and convert it into bits and reverse the string
May 13 '10 #1
12 1921
code green
1,726 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. back = size;
  2. for(c=0;c<size;c++)
  3. {
  4.     newStr[c] = strArray[size];
  5.     back --;
  6. }
May 13 '10 #2
Dheeraj Joshi
1,123 Expert 1GB
Small correction

Expand|Select|Wrap|Line Numbers
  1. newStr[c] = strArray[back];
  2. back--;
  3.  
Regards
Dheeraj Joshi
May 13 '10 #3
@Sinjanv
but how to convert the string into bits first
May 13 '10 #4
jkmyoung
2,057 Expert 2GB
bits and not bytes?
You can do what they're doing for the first part, but then reverse each character's bits.
pseudo:
Expand|Select|Wrap|Line Numbers
  1. get value c
  2. set newValue = 0
  3. shifts = size of c in bits. 
  4. while (shifts > 0){
  5.   if c & 1  == 1
  6.     newValue++;
  7.   c >>= 1;
  8.   if (shifts > 1)
  9.     newValue <<= 1;
  10.   shifts --;
  11. }
then use newValue.
May 13 '10 #5
donbock
2,422 Expert 2GB
What exactly do you mean by "convert the string to bits"? Please provide an example of an input string and the output you want.
May 13 '10 #6
@donbock
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 character
May 13 '10 #7
donbock
2,422 Expert 2GB
OK, the input string is "sinjanv". You called it a string, so it has to be null-terminated. What is the expected output string? Please be specific, otherwise I'm forced to guess what you mean by "reverse the bits".
May 14 '10 #8
whodgson
542 512MB
The way I understand it this function prints the string in reverse order by accessing each character in memory in the reverse order of its assignment.
Expand|Select|Wrap|Line Numbers
  1. void reverse ()
  2. { char c;
  3. cin.get(c);
  4. if(c=='\n')return;
  5. reverse();
  6. cout<<c;
  7. return;
  8. }
May 14 '10 #9
donbock
2,422 Expert 2GB
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.
May 14 '10 #10
whodgson
542 512MB
Yes...see what you mean.
May 15 '10 #11
@donbock
THE abcd becomes {0x61, 0x62, 0x63, 0x64, 0x00} since the given is in hexadecimal form i need to convert into binary in C one character= 8bits so the 8bits are reversed so that it becomes a new character
May 15 '10 #12
donbock
2,422 Expert 2GB
@Sinjanv
Please stop making me guess.
If "abcd" is the same as {0x61, 0x62, 0x63, 0x64, 0x00} then what is the expected output after that string goes into your program?
May 17 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.