This can be done in two ways.
a.Find the length of the string
b. Use MATLAB built in function strtok to separate the first word and the remaining sentence.
For instance, the input string is ‘Lets Learn together... Happy Reading’.
The variable token will contain ‘Lets’
And the variable remain will contain ‘Learn together… Happy Reading’.
Use while loop to extract all the strings.
c. In the first iteration, the string in the token variable is concatenated with a empty variable ‘t’.
Now t has ‘Hello’.
In the second iteration, token has ‘Learn’ and remain has ‘together… Happy Reading’.
Now the string ‘Learn’ is concatenated with the string t.
d. Here to preserve the space ,the syntax [string2, ‘ ’,string1] is used.
The length of the input_string gets reduced during each iteration and finally when all the strings are extracted, the loop is terminated.
%To reverse the words position in a string
str='Lets Learn together... Happy Reading';
Reading Happy together... Learn Lets
This differs in the way of concatenation.
The input string position is reversed. And inside the loop after extracting each token, it is again reversed.
Then the string ‘t’ is concatenated with the token. The first method itself sufficient and just know the other way also.