Software Development

Reverse a String in Dart

In this article, we will look at string reversal in Google’s Dart language.

How to Implement the Solution

Below is the completed Dart solution:

1
2
3
4
5
6
void main(List<String> args) {
    print( reverse(args[0]) );
}
String reverse(input) {
    return input.split('').reversed.join();
}

Much like C or Java, Dart uses the function name main as an entry point for the program. In this case, we don’t need main to return any data so the type void is used. Within the main function’s parameters, you’ll see List<String> args which will define args as a list of strings and fill this list with input from the command line in the form of arguments which in this case are simply strings separated by spaces.

For example:

1
dart reverse-string.dart "Hello World"

…would fill the args list as [Hello World].

1
dart reverse-string.dart "Hello" "World"

…would fill the args list as [Hello, World].

It should be noted that in this example program only the first argument passed to the program will be processed because we’re only passing the first string in the list to our reverse function’s input by adding the index position [0] to args in the print( reverse(args[0]) ); line. So, in the second example above, only Hello would get reversed.

So let’s look at that reverse function. We’ve defined the function with type String as it’ll return our reversed string to be printed to the console. It takes the parameter input as an undefined variable but we’re assuming here that it will be a string value.

The Dart:core library String class contains the split method which will split a string or list of strings into a list of substrings based on the pattern given to it. If the pattern is empty as in our reverse function, split will break up the input into single-code unit strings (individual characters). So ‘Hello’ with the empty pattern '' returns [H, e, l, l, o].

After we’ve broken the string into characters with split, we’ll use the reversed property from the Dart:core library’s List class. This property simply takes a list and returns it in reversed order as an Iterable object. So our example list, [H, e, l, l, o], becomes (o, l, l, e, H).

For the purposes of this sample program, there is very little difference between List and Iterable objects. Both are in fact iterable. At a high level, lists will have additional functionality such as indexed read/write access to its elements and sorting functions. Iterables on the other hand are typically created once then accessed as readonly data during an iteration operation like a for-loop.

Both Iterables and Lists have the join method which will take their elements and concatenate them into a string. We use join here on the reversed Iterable which is returned as the final string value of our reverse function. This value is then printed out to the console from print function within main.

How to Run Solution

To run the Dart string reversal program, download the dart file from GitHub, install the Dart SDK as described at dart.dev, and run the following from the command line:

1
dart reverse-string.dart "Hello, World!"

Alternatively, you can copy the source code into DartPad, an online Dart interpreter. Just keep in mind that you won’t have access to the command line arguments input using this method so you’ll have to populate the args variable in the code instead. For example:

1
2
3
4
5
6
7
void main(List<String> args) {
    args = ['Hello World'];
    print( reverse(args[0]) );
}
String reverse(input) {
    return input.split('').reversed.join();
}

1


References

  1. P. Ryon (slashdoom), “Reverse a String in Dart,” The Renegade Coder, 16-Oct-2019. [Online]. Available: https://therenegadecoder.com/code/reverse-a-string-in-dart/. [Accessed: 16-Oct-2019]. 

sample-programs is maintained by TheRenegadeCoder.

Published on Java Code Geeks with permission by Jeremy Grifski, partner at our JCG program. See the original article here: Reverse a String in Dart

Opinions expressed by Java Code Geeks contributors are their own.

Jeremy Grifski

Jeremy is the founder of The Renegade Coder, a software curriculum website launched in 2017. In addition, he is a PhD student with an interest in education and data visualization.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button