Edit: Just like Joe above, I also thought the initial question was asking for the word length, not index. Here's my response in R.
By Index
library(tidyverse)
library(glue)
str_org <- "I love the power of Grep SED and AWK, but I am no good at using it" %>% str_split(" ")
str <- as.vector(glue("({1:length(str_org[[1]])})"))
str <- glue_collapse(paste(str_org[[1]],as.vector(str)), sep = " ")
print(str)
Result
I (1) love (2) the (3) power (4) of (5) Grep (6) SED (7) and (8) AWK, (9) but (10) I (11) am (12) no (13) good (14) at (15) using (16) it (17)
By Word Length
library(tidyverse)
library(glue)
str_org <- "I love the power of Grep SED and AWK, but I am no good at using it" %>% str_split(" ")
str <- as.vector(glue("({str_count(str_org[[1]])})"))
str <- glue_collapse(paste(str_org[[1]],as.vector(str)), sep = " ")
print(str)
The result
I (1) love (4) the (3) power (5) of (2) Grep (4) SED (3) and (3) AWK, (4) but (3) I (1) am (2) no (2) good (4) at (2) using (5) it (2)
By Word Length, Not Counting Periods or Commas
library(tidyverse)
library(glue)
str_org <- "I love the power of Grep SED and AWK, but I am no good at using it" %>% str_split(" ")
str_count <- str_count(str_replace(str_org[[1]],c(",|\\."),""))
str <- as.vector(glue("({str_count})"))
str <- glue_collapse(paste(str_org[[1]],as.vector(str)), sep = " ")
print(str)
This post is in no obvious way related to bioinformatics. It is not even related to biology. Given how quickly in most circumstances the moderators shut down unrelated posts - and even post related to biology but not to bioinformatics - I can only conclude that you are showing off. That will surely offend someone who has already contributed in this thread - and I am grateful for your useful code - but think about how many posters that have much better case than this one are offended when you close their threads.
You are right that this question is probably offtopic (OP has been posting bioinformatic questions before, leading me to assume there must be some biological context here.), and could have been closed for that reason. It is, however, a delicate balance between "things we don't want on biostars" and "things which are not the main focus but for which the community can easily help ". Personally I think pure coding questions fit better here than biology, but that's disputable. Surely it is interesting to see how such a thing can be done in multiple languages - but that's also out of scope for biostars.
Agreed! It's also nice to have a bit of fun on here. Seems like we got a nice collection of people approaching this with their preferred language. And it got me to procrastinate some work for a couple of more minutes! :)
You are right, and I was inches away from closing the post outright. I wrote a solution out of my own interest which I wasn't going to post. We started to discuss on the Slack group and others provided some comparable code. It was decided at that point that we might as well post them since people had written them for their own edification.
But make no mistake, this thread is not forum-suitable in its current form. It may be some sort of legitimate biological/bioinformatic task that has been heavily abstracted, but if thats the case, that is why I asked OP to elaborate.
Threads like this should and will remain the exception rather than the rule however.
Can you explain how this is a bioinformatics question and why you'd want to do this?