What is logic behind Getting the string with max length from the strings that even length in r

string

#1

how to find the word which is having maximum length in a sentence ,and which is in even position ?
ex: " this is a table"
here we need to get output as - “table”.


#2
sam="this is a table"
list=sam.split()
max_length=len(list[0])
pos=1
max_word=list[0]
for word in list:
    if (len(word)>max_length) & (pos%2==0):
        max_length=len(word)
        max_word=word
    pos+=1

print(max_word)

Hope this helps.


#3

Similar code in R would look like:

a = 'this is a table'
b = strsplit(a, "\\s+")[[1]]
i = 1
y = vector(mode="character", length=1)
for(i in 1:length(b)){
    if((i%%2 == 0) & (nchar(b[i])==max(nchar(b)))){
           y = b[i]
           }
   }
print(y)

Here, the argument ‘’\s+’’ makes the string split applicable even when you have variable spaces in a sentence.

Hope this was helpful.