Flipping specific segment of string rather than the whole string

1 回表示 (過去 30 日間)
Samiha Shimla
Samiha Shimla 2019 年 1 月 17 日
コメント済み: Samiha Shimla 2019 年 1 月 17 日
If I have a string
str='One two buckle my shoe'
how can I flip it to look as follows?
strM='enO owt elkcub ym eohs'
I have tried using 'flip' and 'reverse' in a few different ways but every time I get
strN='eohs ym elkcub owt enO'
  2 件のコメント
Jan
Jan 2019 年 1 月 17 日
This sounds like a homework question. Please mention this explicitly, because then we can help you to solve the problem by your own. This is better than submitting a solution written by someone else.
Samiha Shimla
Samiha Shimla 2019 年 1 月 17 日
Hey, rest assured this is not homework. It is a section of a past paper I am solving. I was specifically struggling with this bit. Thanks for the help!

サインインしてコメントする。

回答 (3 件)

Stephen23
Stephen23 2019 年 1 月 17 日
regexprep(str,'\w+','${fliplr($0)}')
  2 件のコメント
Jan
Jan 2019 年 1 月 17 日
編集済み: Jan 2019 年 1 月 17 日
+1. Compact and clear!
Sean de Wolski
Sean de Wolski 2019 年 1 月 17 日
Nice use of a function in a regexp!

サインインしてコメントする。


madhan ravi
madhan ravi 2019 年 1 月 17 日
編集済み: madhan ravi 2019 年 1 月 17 日
str='One two buckle my shoe';
b=strsplit(str,' ');
strM=strjoin(cellfun(@flip,b,'un',0))
b=flip(strsplit(str,' '));
strN=strjoin(cellfun(@flip,b,'un',0))
Gives:
str =
'One two buckle my shoe'
strM =
'enO owt elkcub ym eohs'
strN =
'eohs ym elkcub owt enO'
  4 件のコメント
Samiha Shimla
Samiha Shimla 2019 年 1 月 17 日
Thanks a lot! This makes sense now! ^_^
madhan ravi
madhan ravi 2019 年 1 月 17 日
Anytime :)

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2019 年 1 月 17 日
str=join(reverse(split("One two buckle my shoe")))
  1 件のコメント
Samiha Shimla
Samiha Shimla 2019 年 1 月 17 日
This is an excellent way of doing it! Thanks Sean!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by