How to reverse the order of words in string
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mekala balaji
am 14 Jan. 2020
Beantwortet: Image Analyst
am 14 Jan. 2020
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 Kommentar
Geoff Hayes
am 14 Jan. 2020
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.
Akzeptierte Antwort
Meg Noah
am 14 Jan. 2020
There's undoubetly better solutions, but:
strtrim(strrep(cell2mat([flipud(split('Hello. I. am. Here.'))']),'.','. '))
0 Kommentare
Weitere Antworten (1)
Image Analyst
am 14 Jan. 2020
Sounds like homework, so here is a hint towards the solution:
str = 'Hello. I. am. Here.'
words = strsplit(str)
reverseWords = words(end:-1:1)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!