Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding the nth word in a given string?

Subject: Finding the nth word in a given string?

From: Daniel

Date: 8 Apr, 2009 07:14:01

Message: 1 of 6

Hi,

How can I write a program that finds and displays the nth word in any given text string?

I have made a few attempts already by exploiting the fact that words are seperated by a single space, however I still cannot figure out how to find the nth word when n=1 (the first word in the string), or when n equals the last word in the string.

Although I did manage to find a way to pick the first word, I'm sure there is a much more succinct method, and one that can pick the last word as well.

Also,
-- Is it possible do a "quick fix" on the code and just manipulate an input statement so that spaces automatically enclose whatever the user inputs (so that now the 1st and last words are surrounded by two spaces)? How can I do this to an input statement?

-- Can I try a different approach where the program reads letters in the string one by one, cataloging space locations and thus indexing the start/finish of each word? Maybe with the isspace MATLAB function?

Here is my first attempt (has no input statements though):

% finding the nth word in a given string

clear;
clc;

str = 'Pass me the oceanic repellant';
n = 4; % nth word
space = findstr(str, ' ');

if n == 1
    wordstart = 1;
else wordstart = space(n-1)+1; % +1 to rid of the space preceding the word
end

wordend = space(n)-1; % -1 to rid of the space following the word

nthword = str(wordstart:wordend);
disp(nthword);



Thank you very much for your help, I am very new to MATLAB...

Daniel

Subject: Finding the nth word in a given string?

From: Bruno Luong

Date: 8 Apr, 2009 07:42:02

Message: 2 of 6

"Daniel " <simpson.d@hotmail.co.uk> wrote in message <grhirp$qgh$1@fred.mathworks.com>...
> Hi,
>
> How can I write a program that finds and displays the nth word in any given text string?
>
> I have made a few attempts already by exploiting the fact that words are seperated by a single space, however I still cannot figure out how to find the nth word when n=1 (the first word in the string), or when n equals the last word in the string.
>
> Although I did manage to find a way to pick the first word, I'm sure there is a much more succinct method, and one that can pick the last word as well.
>
> Also,
> -- Is it possible do a "quick fix" on the code and just manipulate an input statement so that spaces automatically enclose whatever the user inputs (so that now the 1st and last words are surrounded by two spaces)? How can I do this to an input statement?
>
> -- Can I try a different approach where the program reads letters in the string one by one, cataloging space locations and thus indexing the start/finish of each word? Maybe with the isspace MATLAB function?
>
> Here is my first attempt (has no input statements though):
>
> % finding the nth word in a given string
>
> clear;
> clc;
>
> str = 'Pass me the oceanic repellant';
> n = 4; % nth word
> space = findstr(str, ' ');
>
> if n == 1
> wordstart = 1;
> else wordstart = space(n-1)+1; % +1 to rid of the space preceding the word
> end
>
> wordend = space(n)-1; % -1 to rid of the space following the word
>
> nthword = str(wordstart:wordend);
> disp(nthword);
>
>
>
> Thank you very much for your help, I am very new to MATLAB...
>

The best tool for this kind of string manipulation is REGEXP. Regular expressions are hard to use but very powerful.

Bruno

Subject: Finding the nth word in a given string?

From: Nasser Abbasi

Date: 8 Apr, 2009 07:49:35

Message: 3 of 6


"Daniel " <simpson.d@hotmail.co.uk> wrote in message
news:grhirp$qgh$1@fred.mathworks.com...
> Hi,
>
> How can I write a program that finds and displays the nth word in any
> given text string?
>
> Thank you very much for your help, I am very new to MATLAB...
>
> Daniel

Can't you just textscan it and then Then pick any item by its position ?
like this may be

clear all
str = 'Pass me the oceanic repellant';
C = textscan(str,'%s');
data=cellstr(C{1});
for i=1:length(data)
    fprintf('Item number [%d] is "%s"\n',i,char(data(i)))
end

Item number [1] is "Pass"
Item number [2] is "me"
Item number [3] is "the"
Item number [4] is "oceanic"
Item number [5] is "repellant"

good luck

--Nasser

Subject: Finding the nth word in a given string?

From: trancemissionxxi@gmail.com

Date: 8 Apr, 2009 07:53:56

Message: 4 of 6

On Apr 8, 12:14=A0am, "Daniel " <simpso...@hotmail.co.uk> wrote:
> Hi,
>
> How can I write a program that finds and displays the nth word in any giv=
en text string?
>
> I have made a few attempts already by exploiting the fact that words are =
seperated by a single space, however I still cannot figure out how to find =
the nth word when n=3D1 (the first word in the string), or when n equals th=
e last word in the string.
>
> Although I did manage to find a way to pick the first word, I'm sure ther=
e is a much more succinct method, and one that can pick the last word as we=
ll.
>
> Also,
> -- Is it possible do a "quick fix" on the code and just manipulate an inp=
ut statement so that spaces automatically enclose whatever the user inputs =
(so that now the 1st and last words are surrounded by two spaces)? How can =
I do this to an input statement?
>
> -- Can I try a different approach where the program reads letters in the =
string one by one, cataloging space locations and thus indexing the start/f=
inish of each word? =A0Maybe with the isspace MATLAB function?
>
> Here is my first attempt (has no input statements though):
>
> % finding the nth word in a given string
>
> clear;
> clc;
>
> str =3D 'Pass me the oceanic repellant';
> n =3D 4; % nth word
> space =3D findstr(str, ' ');
>
> if n =3D=3D 1
> =A0 =A0 wordstart =3D 1;
> else wordstart =3D space(n-1)+1; % +1 to rid of the space preceding the w=
ord
> end
>
> wordend =3D space(n)-1; % -1 to rid of the space following the word
>
> nthword =3D str(wordstart:wordend);
> disp(nthword);
>
> Thank you very much for your help, I am very new to MATLAB...
>
> Daniel

Why not:

result=3Dregexp(str,'(?<word>\w+)\s*|\s*(?<word>\w+)','names');

then your nth word is:

result(n).word

Subject: Finding the nth word in a given string?

From: Jos

Date: 8 Apr, 2009 08:38:01

Message: 5 of 6

"Daniel " <simpson.d@hotmail.co.uk> wrote in message
<SNIP .. wants to find the Nth word in a string

A simple two-step algorithm:
1) read in the string as separate words
2) select the Nth word

Example:
% data
  str = 'Pass me the oceanic repellant';
  n = 4; % nth word
% engine
  C = strread(str,'%s')
  NthWord = C{n} ;

You might need to learn how to use cell arrays.

hth
Jos

Subject: Finding the nth word in a given string?

From: Pekka Kumpulainen

Date: 8 Apr, 2009 09:39:01

Message: 6 of 6

trancemissionxxi@gmail.com wrote in message <a4e45c74-3b5c-456d-a36e-e13c06344cb1@3g2000yqk.googlegroups.com>...
>
> Why not:
>
> result=3Dregexp(str,'(?<word>\w+)\s*|\s*(?<word>\w+)','names');
>
> then your nth word is:
>
> result(n).word

Wow, that's real regexp acrobatics.

Why not simply:
g = regexp(str, ' ', 'split');
and nth word is in g{n}

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us