Skip to main content
gamedev.net
🔒 Locked

Reverse string

Started by Database 6 replies 950+ views
Original Post
Database
Database
Hey, I want to write a program that uses character arrays and string to reverse a given string. 1) How can I create a string on the fly. I mean how can reserve memory for my string so that I don't have to define the lengh of it at the beginning. 2) How would a loop look like, which basically switches all the character in the given string to reverse? Thanks in advance
SiCrane
SiCrane
What programming language are you using?
Frequency
Frequency
Quote:
Original post by Database
Hey,

I want to write a program that uses character arrays and string to reverse a given string.

1) How can I create a string on the fly. I mean how can reserve memory for my string so that I don't have to define the lengh of it at the beginning.

2) How would a loop look like, which basically switches all the character in the given string to reverse?

Thanks in advance

Assuming C++:
1) #include <string> so you can use std::string which will manage all of that for you automatically. Makes life a lot easier.

2) Let's say you have an array of chars that are indexed (0 .. 9). You'll want to flip 0 and 9, 1 and 8, 2 and 7, 3 and 6...thinking about how to make a loop that will do this is a great exercise.

Of course if you're using Ruby everything is a lot easier:
print "My string!".reverse
It only takes one mistake to wake up dead the next morning.
Will F
Will F
Here's an example (assuming C++)

#include <string>#include <algorithm>#include <iostream>int main(){  std::string foo("A string");  std::reverse( foo.begin(), foo.end() );  std::cout << foo << "\n";}
Database
Database
Thanks guys, great help!!!!
Database
Database
Well, I want to do it this way:

CODE:

#include <string>
#include <stddef.h>
#include <iostream>

using namespace std;



int main(int argc, char* argv[])
{

char str[] = "Hello";

const size_t len = strlen(str);



char tempStr[len]; // line 18

strcpy(tempStr, str);

for(int i=0; i<len; ++i)
str = tempStr[len - i];


cout << len;
return 0;
}


CODE END:

However, I get these errors:

(18) : error C2057: expected constant expression
(18) : error C2466: cannot allocate an array of constant size 0
(18) : error C2133: 'tempStr' : unknown size

I don't know what to do. Can ya'll help me?
wild_pointer
wild_pointer
Quote:
Original post by Database
Well, I want to do it this way:

CODE:

#include <string>
#include <stddef.h>
#include <iostream>

using namespace std;



int main(int argc, char* argv[])
{

char str[] = "Hello";

const size_t len = strlen(str);



char tempStr[len]; // line 18

strcpy(tempStr, str);

for(int i=0; i<len; ++i)
str = tempStr[len - i];


cout << len;
return 0;
}


CODE END:

However, I get these errors:

(18) : error C2057: expected constant expression
(18) : error C2466: cannot allocate an array of constant size 0
(18) : error C2133: 'tempStr' : unknown size

I don't know what to do. Can ya'll help me?


You need to know how much stack space you'll need at compile time, which isn't really possible here. As the error says, the compiler expects a constant as the size of the array, not a variable.

What was wrong with Will F's solution? It is by far the best. If you don't want to use the standard algorithm for whatever reason, then at least use std::string so that the memory management is done for you. If even that is not possible then I have to assume this is school work, in which case you should look at your notes concerning the heap and/or dynamic allocation of arrays or memory, and particularly the keyword new.

[Edited by - wild_pointer on February 19, 2006 2:27:21 PM]
[size=2]
Zahlman
Zahlman
Keep in mind that if you're ever asked to reverse a string in place, on a low level (i.e. using pointers and so on as you'd like - and it's a reasonable interview question), that what you're doing there is exactly what is not asked for. "In place" means modifying the contents of 'str', without some dynamic allocation. You will need one extra byte, but anyway...

What is requested in that case is that you swap the zeroth and last characters (get in the habit of counting starting at zero - now!), first and next-to-last, etc. Again this is something where the standard library provides what you need - see std::swap - but then if you weren't going to use std::reverse in the first place, then you may as well write this part yourself too.

Consider what happens, though: you want to copy location X over to location Y, and also copy location Y over to location X, so that they are swapped. Which would you like to do first? Oops - whichever you pick, if you just do it that way, then you clobber one of the values before you can copy it back. To swap properly, you have to back up one of the values first. And indeed, std::swap is likely to be written just like it sounds:

// not guaranteed to correspond to any actual real-world implementation :)// If you are really curious, try to look it up in the headers that came// with your compiler.template <typename T>void swap(T& a, T& b) {  T c(a); // copy-initialize in order to back it up  b = c; // requires that the type in question is assignable  a = b;}


In your case, the things being swapped would just be chars, so there are no problems using std::swap (or the equivalent code in-line).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.