如何在MATLAB中反转字符串?
在本文中,我们将讨论 MATLAB 中的“字符串反转”,它可以通过多种方法完成,如下所示:
方法一:使用内置函数
通过使用 MATLAB 内置函数,如 reverse()、flip() 和 fliplr()。内置函数可以定义为——“已经在编程框架中定义并执行特定任务的函数”。
使用反向()
reverse()函数用于反转指定字符串的字符顺序。
Syntax: reverse(string)
Parameters: This function accepts a single parameter.
string: This is the specified string whose characters are going to be reversed.
例子:
Matlab
% MATLAB code for reverse()
% Initializing a string
string = 'GeeksforGeeks';
% Calling the reverse() function
% over the above string to
% reverse its characters
Reversed_String = reverse(string)
Matlab
% MATLAB code for reverse
% string using flip()
% Initializing a string
string = 'GeeksforGeeks';
% Calling the flip() function
% over the above string to
% reverse its characters
Reversed_String = flip(string)
Matlab
% MATLAB code for reverse
% string using flip()
% Initializing a string
string = ['a' 'b' 'c'
'd' 'e' 'f'
'g' 'h' 'i'];
% Calling the flip() function
% over the above string along with
% first dimensions to
% reverse its elements
Reversed_String = flip(string, 1)
Matlab
% MATLAB code for reverse
% string using fliplr()
% Initializing a string
string = 'GeeksforGeeks';
% Calling the fliplr() function
% over the above string to
% flip its characters from left
% to right
Reversed_String = fliplr(string)
Matlab
% MATLAB code for reverse string
% Initializing a string
string = 'GeeksforGeeks';
% Reversing the above string
Reversed_String = string(end:-1:1)
Matlab
% MATLAB code for reverse
% string using for loop
% Initializing a string
String = "GeeksforGeeks";
% using FOR loop:
NewString = [];
for i = 1:length(String)
NewString = [NewString, String(end-i+1)];
end
% Getting the reversed string
Reversed_String = NewString
输出:
使用翻转()
flip()函数用于翻转指定字符串的顺序。
Syntax: flip(A)
flip(A, dim)
Here,
flip(A) function is used to return an array of the same size as A, but with the reversed order of the elements.
flip(A, dim) function is used to reverses the order of the elements of A along dimension dim. For example, if A is a matrix, then flip(A,1) reverses the elements in each column, and flip(A,2) reverses the elements in each row.
示例 1:
MATLAB
% MATLAB code for reverse
% string using flip()
% Initializing a string
string = 'GeeksforGeeks';
% Calling the flip() function
% over the above string to
% reverse its characters
Reversed_String = flip(string)
输出:
示例 2:
MATLAB
% MATLAB code for reverse
% string using flip()
% Initializing a string
string = ['a' 'b' 'c'
'd' 'e' 'f'
'g' 'h' 'i'];
% Calling the flip() function
% over the above string along with
% first dimensions to
% reverse its elements
Reversed_String = flip(string, 1)
输出:
使用fliplr()
fliplr()函数用于从左到右翻转指定的字符串。
Syntax: fliplr(String)
Parameters: This function accepts a parameter.
String: It is the specified string whose characters are going to be flipped from left to right.
例子:
MATLAB
% MATLAB code for reverse
% string using fliplr()
% Initializing a string
string = 'GeeksforGeeks';
% Calling the fliplr() function
% over the above string to
% flip its characters from left
% to right
Reversed_String = fliplr(string)
输出:
方法 2:使用end 关键字
在这种方法中,使用以下过程反转字符串。这里“ end”表示数组中的最后一个元素,因此“end-1”是数组中最后一个元素的下一个元素。
例子:
MATLAB
% MATLAB code for reverse string
% Initializing a string
string = 'GeeksforGeeks';
% Reversing the above string
Reversed_String = string(end:-1:1)
输出:
方法三:使用for循环
MATLAB 中的基本 for 循环通常用于迭代地分配或访问数组元素。我们也可以使用 for 循环反转字符串。
MATLAB
% MATLAB code for reverse
% string using for loop
% Initializing a string
String = "GeeksforGeeks";
% using FOR loop:
NewString = [];
for i = 1:length(String)
NewString = [NewString, String(end-i+1)];
end
% Getting the reversed string
Reversed_String = NewString
输出: