Reverse a String in MATLAB



MATLAB provides various built-in functions to reverse a string of text. In this tutorial, I am going to explain all the commonly used methods to reverse a string in MATLAB.

Reverse a String Using "reverse" Function in MATLAB

There is a built-in function "reverse" in MATLAB that we can use to reverse a string.

Syntax

reversed_string = reverse(string);

The steps involved in reversing a string using the "reverse" function are given below

  • Step (1) Create a string of text.

  • Step (2) Call the "reverse" function to reverse the string.

  • Step (3) Display the result.

Example

Let us see an example to understand the implementation of code in MATLAB to reverse a string using the "reverse" function.

% MATLAB code to reverse a string using "reverse" function % Declare an example string S = 'Hello Learners, Welcome to Tutorials Point'; % Reverse the string reversed_str = reverse(S); % Display the original and reversed strings disp('The original string is:'); disp(S); disp('The reversed string is:'); disp(reversed_str);

Output

When your run this code, it will produce the following output

The original string is:
Hello Learners, Welcome to Tutorials Point
The reversed string is:
tnioP slairotuT ot emocleW ,srenraeL olleH

Reverse a String Using "flip" Function in MATLAB

There is another built-in function "flip" in MATLAB R2019b and later versions that we can use to reverse a string.

Syntax

reversed_string = flip(string);

The steps involved in reversing a string using the "flip" function are given below

  • Step (1) Create a string of text.

  • Step (2) Call the "flip" function to reverse the string.

  • Step (3) Display the result.

Example

The following example demonstrates how to use the "flip" function to reverse a string in MATLAB.

Open Compiler
% MATLAB code to reverse a string using "flip" function % Create an example string S = 'Hello Learners, Welcome to Tutorials Point'; % Reverse the string reversed_str = flip(S); % Display the original and reversed strings disp('The original string is:'); disp(S); disp('The reversed string is:'); disp(reversed_str);

Output

When your run this code, it will produce the following output

The original string is:
Hello Learners, Welcome to Tutorials Point
The reversed string is:
tnioP slairotuT ot emocleW ,srenraeL olleH

Reverse a String Using "fliplr" Function in MATLAB

In MATLAB, the "fliplr" function is also a built-in function that can be used to reverse a string. The steps involved in reversing the string using this function is similar to that of the "flip" function.

Syntax

reversed_string = fliplr(string);

Example

Here is an example showing the use of the "fliplr" function to reverse a string in MATLAB.

Open Compiler
% MATLAB code to reverse a string using "fliplr" function % Define an example string S = 'Hello Learners, Welcome to Tutorials Point'; % Reverse the string reversed_str = fliplr(S); % Display the original and reversed strings disp('The original string is:'); disp(S); disp('The reversed string is:'); disp(reversed_str);

Output

When your run this code, it will produce the following output

The original string is:
Hello Learners, Welcome to Tutorials Point
The reversed string is:
tnioP slairotuT ot emocleW ,srenraeL olleH

Reverse a String Using "for" Loop in MATLAB

We can also reverse a string using the "for" loop in MATLAB.

The step-by-step process for reversing a string using the "for" loop is given below

  • Step (1) Create a string of text.

  • Step (2) Declare an empty string to store the reversed string.

  • Step (3) Define a "for" loop to reverse the string.

  • Step (4) Display the reversed string.

Example

Let us see an example to understand the implementation and execution of this method of reversing a string in MATLAB.

Open Compiler
% MATLAB code to reverse a string using "for" loop S = 'Hello Learners, Welcome to Tutorials Point'; % Declare an empty string to store the reversed string reversed_str = ''; % Define a for loop to reverse the string for i = length(S) : -1 : 1 reversed_str = [reversed_str, S(i)]; end % Display the original and reversed strings disp('The original string is:'); disp(S); disp('The reversed string is:'); disp(reversed_str);

Output

When your run this code, it will produce the following output

The original string is:
Hello Learners, Welcome to Tutorials Point
The reversed string is:
tnioP slairotuT ot emocleW ,srenraeL olleH

Reverse a String Using "end" Option in MATLAB

In MATLAB, we can also use the "end" option to reverse the string.

Here are the steps involved in reversing a string using the "end" option in MATLAB.

  • Step (1) Create a string.

  • Step (2) Reverse the string using the "end" option.

  • Step (3) Display the reversed string.

Example

Let us see an example to understand the implementation and working of this method.

Open Compiler
% MATLAB code to reverse a string using "for" loop S = 'Hello Learners, Welcome to Tutorials Point'; % Reverse the string reversed_str = S(end : -1 : 1); % Display the original and reversed strings disp('The original string is:'); disp(S); disp('The reversed string is:'); disp(reversed_str);

Output

When your run this code, it will produce the following output

The original string is:
Hello Learners, Welcome to Tutorials Point
The reversed string is:
tnioP slairotuT ot emocleW ,srenraeL olleH

Conclusion

This is all about reversing a string in MATLAB. In this tutorial, I explained different methods of reversing a string with the help of examples in MATLAB programming. You can also try all these codes with different strings.

Updated on: 2023-10-09T11:14:27+05:30

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements