How to reverse the order of words in string

조회 수: 45 (최근 30일)
Mekala balaji
Mekala balaji . 2020년 1월 14일
답변: Image Analyst . 2020년 1월 14일
I have below sentence and want pring in reverse order;
'Hello. I. am. Here.'
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에 대해 자세히 알아보기





Community Treasure Hunt

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

Start Hunting!

Translated by