Hello! I am attempting to write a program that translates a sentence from a file to Pig Latin. Here is the first sentence that is being translated:
I SLEPT MOST OF THE NIGHT
When the function runs through the loop the first time, it works as expected. When the loop runs a second time, length is being set to 7, instead of 5 as expected. From my understanding, .find should return a 5 since it is searching for the second space in the string from the first space in the string. Instead, it is returning a 7, which sets word to SLEPT M and it all just goes downhill from there.
Any help with this issue would be greatly appreciated! Thank you!
find should return a 5 since it is searching for the second space in the string from the first space in the string.
No. find returns: The position of the first character of the first match.
Line 8: find doesn't return the length of what it found. It returns the position of where the match occurred. First iteration, returns position (of space) = 1, which is correct for the length of the first word. Second iteration, find is called with a starting position of 2. However, find returns the POSITION (from the start of the string) of the next space. That is position 7.