forJ
forJ

Reputation: 4617

How to search for an index of specific word with an index of specific character

Say for example I have the words below

THIS TEXT IS A SAMPLE TEXT

I am given character index 7.

Then I have to return index 1 when I split the sentence into words which is the index of the word that contains the index of character not 5 which matches the word that composes the index of character exactly but not the correct index where character lies.

basically I am trying to return the correct word index of where character lies (when split into words) with character index (when split with characters)

I thought I would reconstruct the word with something like below to find the word at the character

let curString = 'find a word from here';
let initialPositin = 5
let position = initialPositin

let stringBuilder = '';

while(position > -1 && curString.charAt(position) !== ' '){
  console.log('run 1')
  console.log(position);

  stringBuilder = curString.charAt(position) + stringBuilder;

  position --;
}

console.log(stringBuilder)

position = initialPositin + 1;

while(position < curString.length && curString.charAt(position) !== ' '){
  console.log('run 2')

  stringBuilder += curString.charAt(position);

  position ++;
}

console.log(stringBuilder);

Then split the sentence into words then find all the index of the word that contains the word that I have constructed. Then go through all the found words and reconstruct the previous words to see if the index of the target character in the reconstruction matches the character position given.

It doesn't really feel efficient. Does anyone have better suggestions?

I prefer javascript but I can try to translate any other language myself

Upvotes: 1

Views: 977

Answers (5)

Damiaan Dufaux
Damiaan Dufaux

Reputation: 4785

Just like the answer from @miradham this function counts the spaces before the given index, but with builtin functions to count character occurrences.

function wordIndexOfCharacterIndexInString(index, string) {
  const stringUpToCharacter = string.slice(0, index)
  return (stringUpToCharacter.match(/ /g) || []).length
}

console.log(wordIndexOfCharacterIndexInString(7, "THIS TEXT IS A SAMPLE TEXT"))

Upvotes: 0

forJ
forJ

Reputation: 4617

I ended up using below code

let content = 'THIS IS A SAMPLE SENTENCE';
let target = 13;

let spaceCount = 0;
let index = 0;

while(index < target) {

    if (content.charAt(index) === ' ') {
        spaceCount++;
    }

    index++;
}

let splitContent = content.split(' ');
splitContent[spaceCount] = '#' + value
console.log(splitContent.join(' '))

Worked very nicely

Upvotes: 0

Burak Buğrul
Burak Buğrul

Reputation: 74

text = "THIS TEXT IS A SAMPLE TEXT"
indexes = []
current_word = 0

for i in range(0, len(text)):

    if text[i] == ' ':
        current_word += 1  # After a ' ' character, we passed a word
    else:
        indexes.append(current_word)  # current character belongs to current word

You can build indexes array for once with this piece of code(written in Python3) then you can use it for every indice. If you want to count ' ' characters in indexes array as well, you can simple add them in for loop(in if statement).

Upvotes: 0

miradham
miradham

Reputation: 2345

I think you could just count spaces that occurs before given index, something like

let curString = 'find a word from here';
let givenIndex = 9;

let spaceIndex = 0;
for (var i = 0; i < curString.length; i++) {
  if(curString.charAt(i) == ' ') {
      if (i < givenIndex) {
          spaceIndex++;
      } else {
          // found what we need
          console.log(spaceIndex);
      }
  }
}

Upvotes: 3

Jacques Ung
Jacques Ung

Reputation: 157

Maybe you could build a function that returns the position of all spaces. Then you can see where the character index fits in that list of space positions.

Upvotes: 0

Related Questions