How to reverse the order of words in string

11 次查看(过去 30 天)
Hi,
I have below sentence and want pring in reverse order;
Input:
'Hello. I. am. Here.'
Output:
Here. am. I Hello.
I use below code, but both does not work.
>> ss = 'Hello. I. am. Here.'
>> fliplr(ss)
ans =
'.ereH .ma .I .olleH'
>> ss
ss =
'Hello. I. am. Here.'
>> reverse(ss)
ans =
'.ereH .ma .I .olleH'
  1 个评论
Geoff Hayes
Geoff Hayes 2020-1-14
Mekala - you will need to write code that recognizes the words in your sentences and then reverse the order of the words. Using fliplr or reverse will just flip or reverse the characters in your strings/array. Perhaps strtok might be useful.

请先登录,再进行评论。

采纳的回答

Meg Noah
Meg Noah 2020-1-14
There's undoubetly better solutions, but:
strtrim(strrep(cell2mat([flipud(split('Hello. I. am. Here.'))']),'.','. '))

更多回答(1 个)

Image Analyst
Image Analyst 2020-1-14
Sounds like homework, so here is a hint towards the solution:
str = 'Hello. I. am. Here.'
words = strsplit(str)
reverseWords = words(end:-1:1)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by