Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need assistance with string problem  RSS feed

 
Adam Dorian
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey!

So I started this assignment to count the number of occurrences a certain word shows up in a string and removes that occurrence.
The code below works but I need help figuring out how to search the same string again to continue searching for another occurrence of that specific word.

Example:
Word to search: "really"
String: "rerealreallylyally"
Number of Occurences: 3

Help would be much appreciated, Thanks!

 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of your test strings contain the substring "really" more than once. Unless, of course, when you find that string you remove it and then search again from the beginning. But nothing in your code makes an attempt to remove the search word.

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below works but I need help figuring out how to search the same string again to continue searching for another occurrence of that specific word.

I'm confused because it seems like your code does this. Can you explain more about what you want to do?

BTW, you can do this with a Regular Expressions, but if you don't know how to use them, they can take a little getting used to.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:None of your test strings contain the substring "really" more than once.

Well, this does:
"reallytacoteusdayreally"
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But nothing in your code makes an attempt to remove the search word.

He creates a substring with an index past the occurrence of the word, then recurses.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if he were to remove the found substring then the results would be different. For example, with the string "rerealreallylyally" if he removes the first occurrence of "really" you end up with "rereallyally", and recurse on that result by removing "really" to get "really" and recurse again to get an empty string. I agree that it is unclear as to what his desired result is - that is, why he mentioned removing the substring.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take one of your example strings:
Please tell us what is the restOfTheString if index is equal to 6?
Hint: check what is returned if you supply 1 index to substring method.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More hints:
After you first time remove "really", you need to end up with:
Now you left only to work out the maths with indices in order to get whats in the quotes above.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!