1
k = strcmp(word{w},com_word{y});
if(k) new_word = strrep(word,word{w},'');

Say i wanna do something like this , comparing two strings word by word for example:

str1 : ' my world is awesome '

str2 : ' my world was awesome '

Now i want to compare the words in str1 with those in str2 and remove the common words in both. Finally, only ' is ' remains in str1.

1
  • It would really help us if you posted what you've tried and detail what, exactly, isn't working. Welcome to SO! Commented Aug 28, 2014 at 17:35

1 Answer 1

3

Tools that you can use here - regexp, strtrim, setdiff.

Code

str1 = ' my world is awesome '
str2 = ' my world was awesome '

split1 = regexp(strtrim(str1),'\s','Split')
split2 = regexp(strtrim(str2),'\s','Split')

str1_exclusive = setdiff(split1,split2)
str2_exclusive = setdiff(split2,split1)

Output

str1_exclusive = 
    'is'
str2_exclusive = 
    'was'
Sign up to request clarification or add additional context in comments.

9 Comments

yeah , thanks ! but the str1_exclusive and str2_exclusive are in the cells' format, what if we want them to be just strings ? as that what we read from a text file ?
@PriyamSoneji Use char().
sorry to be asking such questions , i'm doing it for the first time, but it gives it in a columnar format only , while displaying , can't it be in a row format ? like our initial strings are ?
@PriyamSoneji That's okay! We learn things as we try them out!
How do we get it to come as an actual string ? As our initial strings were ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.