如何在 MATLAB 中反轉字串?


MATLAB 提供了各種內建函式來反轉文字字串。在本教程中,我將解釋所有常用的在 MATLAB 中反轉字串的方法。

使用 MATLAB 中的 "reverse" 函式反轉字串

MATLAB 中有一個內建函式 "reverse",我們可以用它來反轉字串。

語法

reversed_string = reverse(string);

使用 "reverse" 函式反轉字串所涉及的步驟如下所示:

  • 步驟 (1) − 建立一個文字字串。

  • 步驟 (2) − 呼叫 "reverse" 函式來反轉字串。

  • 步驟 (3) − 顯示結果。

示例

讓我們來看一個示例,瞭解如何在 MATLAB 中使用 "reverse" 函式反轉字串的程式碼實現。

% 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);

輸出

執行此程式碼時,將生成以下輸出

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

使用 MATLAB 中的 "flip" 函式反轉字串

MATLAB R2019b 及更高版本中還有另一個內建函式 "flip",我們可以用它來反轉字串。

語法

reversed_string = flip(string);

使用 "flip" 函式反轉字串所涉及的步驟如下所示:

  • 步驟 (1) − 建立一個文字字串。

  • 步驟 (2) − 呼叫 "flip" 函式來反轉字串。

  • 步驟 (3) − 顯示結果。

示例

以下示例演示瞭如何在 MATLAB 中使用 "flip" 函式反轉字串。

% 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);

輸出

執行此程式碼時,將生成以下輸出

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

使用 MATLAB 中的 "fliplr" 函式反轉字串

在 MATLAB 中,"fliplr" 函式也是一個內建函式,可用於反轉字串。使用此函式反轉字串所涉及的步驟與 "flip" 函式類似。

語法

reversed_string = fliplr(string);

示例

這是一個示例,展示瞭如何在 MATLAB 中使用 "fliplr" 函式反轉字串。

% 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);

輸出

執行此程式碼時,將生成以下輸出

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

使用 MATLAB 中的 "for" 迴圈反轉字串

我們也可以使用 MATLAB 中的 "for" 迴圈來反轉字串。

使用 "for" 迴圈反轉字串的分步過程如下所示:

  • 步驟 (1) − 建立一個文字字串。

  • 步驟 (2) − 宣告一個空字串以儲存反轉後的字串。

  • 步驟 (3) − 定義一個 "for" 迴圈來反轉字串。

  • 步驟 (4) − 顯示反轉後的字串。

示例

讓我們來看一個示例,瞭解此方法在 MATLAB 中反轉字串的實現和執行。

% 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);

輸出

執行此程式碼時,將生成以下輸出

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

使用 MATLAB 中的 "end" 選項反轉字串

在 MATLAB 中,我們還可以使用 "end" 選項來反轉字串。

以下是使用 MATLAB 中的 "end" 選項反轉字串所涉及的步驟。

  • 步驟 (1) − 建立一個字串。

  • 步驟 (2) − 使用 "end" 選項反轉字串。

  • 步驟 (3) − 顯示反轉後的字串。

示例

讓我們來看一個示例,瞭解此方法的實現和工作原理。

% 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);

輸出

執行此程式碼時,將生成以下輸出

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

結論

這就是關於在 MATLAB 中反轉字串的所有內容。在本教程中,我透過 MATLAB 程式設計中的示例解釋了反轉字串的不同方法。您也可以嘗試使用不同的字串執行所有這些程式碼。

更新於: 2023年10月9日

222 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.