Problems with Matlab Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you Matlab Codes for Registered Members of this site only, at free service...Follow Me.

Reverse the words position in a string using matlab


This can be done in two ways.
First Method:
 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’.
[Token,remain]=strtok(input_string);
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.

MATLAB CODE:
%To reverse the words position in a string
str='Lets Learn together... Happy Reading';
%str=fliplr(str);
len=length(str);
t='';

 while(len>0)
     [token,str]=strtok(str);
     len=length(str);
     %token=fliplr(token);
     %t=[token,' ',t];
     t=[' ',token,t];
        
 end
 display(t);


Output:

t =

 Reading Happy together... Learn Lets



Second  Method:

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.

0 comments:

Post a Comment

Recent Comments

Popular Matlab Topics

Share your knowledge - help others

Crazy over Matlab Projects ? - Join Now - Follow Me

Sites U Missed to Visit ?

Related Posts Plugin for WordPress, Blogger...

Latest Articles

Special Search For Matlab Projects

MATLAB PROJECTS

counter

Bharadwaj. Powered by Blogger.