How to reverse a string?

@bendqh1 What database server are you using?

I use MySQL and MariaDB indirectly on hosting platforms but I donā€™t program in SQL querying language. The last time I wrote a SQL query was maybe year 2017.

I am looking for something less ā€œnicheā€.

  1. I meanā€¦ itā€™s not a ā€œplethoraā€, its an organized array of a fixed size equal to the length of the original string. Weā€™re not just throwing out extra characters for the sake of it :stuck_out_tongue:

I am very reluctant call it that and would prefer to say perhaps:

A new and reorganized string of characters of growing length (size) which in the end of the process equals the length (size) of the first string.

Do you get paid by the word? :lol:

1 Like

No. Perhaps I should try that again:

  1. Split a string to individual characters.
  2. Take each individual character and put it after the last individual character.
  3. Join the reversed individual characters to a new string.

We get a new, reorganized string of reversed characters which grow in size (length) each time a character was moved after the last character until getting the new string of the same size (length).

I was going to say that this sounds like a terrible idea, but then realized they are talking about functions/procedures and changed my mind. Interesting concept indeed. Thanks for the share. :slight_smile:

1 Like

Thatā€™s essentially right.

I do want to say something. It may seem like youā€™re being ganged up upon, but in reality, weā€™re trying to reduce the work you need to do.

Iā€™m an old school programmer (more than 30 yrs of professional experience from Mainframe COBOL and flat files to thick client apps to modern web current technologies). I learned all the paradigms for sorting and looping and such as part of my education - but thatā€™s because the languages didnā€™t have built in methods to do those types of things.

Now, almost ALL modern languages, whether scripted, compiled or database, all have in-built methods for doing rudimentary operations. If they didnā€™t, no one would use them because why worry about the basics when you can spend your time implementing features that help your customers/clients?

But if you really want to understand the fundamentals, do some searching for sort design patterns. Theyā€™re still out there (bubble sort for the win!) but most just donā€™t take the time to worry about how they work anymore. Just that they do.

Speaking as if he was Billy Crystal in the Princess Brideā€¦

ā€œYou promised me you wouldnā€™t say that name!ā€
ā€œCOBOL COBOL COBOL!ā€

2 Likes

To be metaphoric for a moment, I like to give shallow water dives (if not deeper) from time to time, to computer science and software logic, in my spare time :slight_smile:

Unfortunately, that may be a bit too Jack Handey as thatā€™s ā€œHow does X work?ā€ vs most things here are ā€œHow do I do Y?ā€ to which people reply ā€œYou can use X (or Z in that language or AFQ or whatever)ā€

i feel seen

1 Like

Oh, I didnā€™t mean here necessarily but in generalā€¦

As I said, it is not necessary to iterate for the same amount of iterations as there are characters.

I think a binary search is easy and much faster. In 1973 I modified a COBOL program to use a binary search and to dynamically add new elements when encountered.

To return to the subject of reversing a string, please see: https://dotnetfiddle.net/gxemv6

That page is a fiddle, it will show you the code and execute the code and the output is at the bottom. The important part of the code is:

StringBuilder from = new StringBuilder("+54321");
Console.WriteLine(from);
for (int i=0; i<(from.Length>>1); ++i)
{
	char c = from[i];
	int ii = from.Length - i - 1;
	Console.WriteLine("{0} {1} {2}", i, ii, c);
	from[i] = from[ii];
	from[ii] = c;
}
Console.WriteLine(from);

The output is:

+54321
0 5 +
1 4 5
2 3 4
12345+

That is C#. The code could be written in C and C++ in very much the same way. Note that from.Length>>1 will shift the length by one, thereby dividing by 2. You can modify the code and run the modification using the Run button. See what happens when you change +54321 to +654321. It will still loop 3 times since we need only 3 iterations.

@SamuelCaliforniaā€™s Binary script translated to JS

const reverseString = function(strg) {
  // convert strg to an array
  const target = [...strg];
  const len = strg.length;
  const middle = len >> 1;
  
  for (let ltIndex = 0; ltIndex < middle; ltIndex += 1) {
    const rtIndex = len - ltIndex - 1;
    // swap with destructuring
    [target[ltIndex], target[rtIndex]] = [target[rtIndex], target[ltIndex]];
  }
  return target.join('');
};

console.log(reverseString('+654321')) // 123456+
2 Likes

Which, for clarity:
Daveā€™s version the user rejected:
let reversed = number.split('').reverse().join('');
rpgā€™s version:

const reverseString = function(strg) {
  // convert strg to an array
  const target = [...strg];
  const len = strg.length;
  const middle = len >> 1;
  
  for (let ltIndex = 0; ltIndex < middle; ltIndex += 1) {
    const rtIndex = len - ltIndex - 1;
    // swap with destructuring
    [target[ltIndex], target[rtIndex]] = [target[rtIndex], target[ltIndex]];
  }
  return target.join('');
};

const target = [...strg]; is number.split('')


 const len = strg.length;
  const middle = len >> 1;
  
  for (let ltIndex = 0; ltIndex < middle; ltIndex += 1) {
    const rtIndex = len - ltIndex - 1;
    // swap with destructuring
    [target[ltIndex], target[rtIndex]] = [target[rtIndex], target[ltIndex]];
  }

is .reverse()


and
return target.join('');

ā€¦well unsurprisingly, thatā€™s .join(''), because itā€™s the same function.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.