how to split strings equally with a rest

i want to split a string in equal parts like in this example:input='abcdefghijk' output='abc def ghi jk'

 Accepted Answer

madhan ravi
madhan ravi on 14 Oct 2018
Edited: madhan ravi on 14 Oct 2018
input_string ='abcdefghijk' % don’t use variable name as input because it may contradict with built-in function
ns = numel(input_string);
n = 3;
Output = cellstr(reshape([input_string repmat(' ',1,ceil(ns/n)*n-ns)],n,[])')' % as cell
Output = char(Output) % as char

3 Comments

madhan ravi
madhan ravi on 14 Oct 2018
Edited: madhan ravi on 14 Oct 2018
Make sure to accept and also give a vote if it was useful the answer if it helped
Please, in your answer, correct bad mistakes that the poster made, like calling the variable by the name of a built-in function like input() -- don't let them pass by without pointing them out.
Thank you @sir Image Analyst rectified :)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 14 Oct 2018

Edited:

on 14 Oct 2018

Community Treasure Hunt

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

Start Hunting!