473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reversing a String

How to reverse the string into one array in that array string is store in c language ?

I want to reverse the string in that array in which already stored...using only one array in program
Sep 28 '11 #1
7 2748
Frinavale
9,735 Expert Mod 8TB
  1. Retrieve the length of the string and create an array of that length for storing the reversed characters
  2. Declare 2 loop counters: one for keeping track of the position in the string and the other for keeping track of the position in the array.
  3. Set the loop counter for keeping track of the position in the array to 0
  4. Set the loop counter for keeping track of the position in the string to the length of the string
  5. Implement a loop that uses the counter that keeps track of the position of in the string which moves towards the 0th index position in the string by decrementing the counter
  6. Retrieve the character at that position
  7. Add the character to the array using at the index that is stored in the loop counter used for keeping track of the position in the array
  8. Increment the loop counter for keeping track of the position of the array.
  9. Loop to step 5 (which will decrement the position in the string until 0 is reached)

-Frinny
Sep 28 '11 #2
Frinavale
9,735 Expert Mod 8TB
The above solution only uses 1 array and the String.
If what you mean by "uses only 1 array" refers to the String itself (which is just an array of characters), then my above solution will not work for you.

The solution that you need will require shifting the characters until the are all reversed.

You could use a loop for this purpose...or you could implement a recursive function (a function that calls itself until some condition is met).

-Frinny
Sep 28 '11 #3
whodgson
542 512MB
One way would be to use the reverse() function defined in the header <algorithm>

Expand|Select|Wrap|Line Numbers
  1. char s[] = "abcde fghij";
  2.     cout<<"print string s \n";
  3.     cout<<s;//prints abcde fghij
  4.     reverse(s,s+11);
  5.     cout<<"\nprint reversed string s \n";
  6.     cout<<s;//prints jihgf edcba
  7.  
Sep 29 '11 #4
donbock
2,426 Expert 2GB
  1. Draw a series of perhaps 5 boxes such that they form a row. This is your array variable. Number the boxes starting from zero, these are the array indices.
  2. Populate your boxes with small distinguishable items (differing denominations of coins, pieces of candy, etc). These are the contents of your array.
  3. Draw one box off to the side. This is a temporary variable that you may need.
  4. Save a record of the starting arrangement of your array contents so you can check your results.
  5. Now, reverse the array contents with the following rule: an array content can only be moved to an empty box.
  6. Do this for both an odd and even sized array.
Pay attention to how you accomplished this. That's your algorithm. It is a lot easier to write a program once you truly understand the algorithm.

Are you reversing an array or a string?
If it is a string, then draw one more box at the end of the array and write "\0" in it. Don't move anything in or out of that box because you still need your reversed string to be null-terminated.

Pay attention to how you known when you're finished. If you keep going past the middle element you'll end up de-reversing the array.
Sep 29 '11 #5
whodgson
542 512MB
Another way after finding the length of a string of characters may be to position one pointer at the first character and a second pointer at the last character (the one before the NULL character) then use the swap() function defined in the header <algorithm> to swap these two characters before incrementing ptr#1 and decrementing ptr#2. Continue until the pointers reach the string center without passing.
Sep 30 '11 #6
weaknessforcats
9,208 Expert Mod 8TB
It's already reversed. Just display it in the other direction.
Oct 2 '11 #7
gtoal
2
(elegantly...) string a in words the reversing is problem interesting more much A
Oct 12 '11 #8

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

Similar topics

24
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include...
5
by: Shwetabh | last post by:
Hi, How can I remove a part of string from complete string in SQL? this is something i want to do: update ace set PICTURE = PICTURE - '\\SHWETABH\Shwetabh\I\' But here ' - ' is not...
4
by: Jonathan Wood | last post by:
Is it just me? It seems like one moving from MFC to C# loses some string functionality such as the two mentioned in the subject. Did I miss something? -- Jonathan Wood SoftCircuits...
4
by: hello12 | last post by:
Hello, I am new to java and i was having a hard time figuring out on how to do certain string manipulations. I was asked to read in a text file and reverse the words. So far, I have put all...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
7
by: Suyash Upadhyay | last post by:
Hello all, I encountered a programming problem while going through interview questions asked by companies. But I stuck at this: Given an array of characters. How would you reverse it? How would...
16
by: Scott | last post by:
Yeah I know strings == immutable, but question 1 in section 7.14 of "How to think like a computer Scientist" has me trying to reverse one. I've come up with two things, one works almost like it...
20
by: nholdzkie | last post by:
how to make a program that will reverse the inputed data in output??? ex. the inputed data is blue the output should be eulb
16
by: python101 | last post by:
I intend use string and reverse function to build a simple application in python for DNA (presented by A,G,G, and T) mutation when one of its substring is reversed during the replication process. The...
1
by: rajkumarbathula | last post by:
Hi Could any one help me out in reversing rows/elements of DataTable or String or DataList by using any simple statement? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.