Expected Run
stringBits("Hello") → "Hlo" "He" X
stringBits("Hi") → "H" "Hi" X
stringBits("Heeololeo") → "Hello" "He" X
stringBits("HiHiHi") → "HHH" "Hi" X
stringBits("") → "" "null" X
stringBits("Greetings") → "Getns" "Gr" X
stringBits("Chocoate") → "Coot" "Ch" X
stringBits("pi") → "p" "pi" X
stringBits("Hello Kitten") → "HloKte" "He" X
stringBits("hxaxpxpxy") → "happy" "hx" X

comparing my solution with your solution as above both looks almost similar except in the increment i wrote i=i+1 and inside for loop i=i+2 (where as yours i=i+2 and i=i+1) . I wonder what is the difference.

I also made my usual mistake
i<str.length()-1

I though i represent like index so it is 1 less than original length.

Right well consider this - the word "Hello". The index starts at zero as you say, so this is character 'H'. Then we add 2 to that, so that points at character 'l', then, because 'y' is now 2, and 2 is still less than 5 (the length of the string 'Hello'), we again add 2 to 'y', making it 4, and take the 4th index character, which is 'o', and so we get 'Hlo'. Now, if 2 is added to 'y', it will exceed the string length, and so this brings the loop to a halt.

1. What is the index number of the beginning of the string?
>>0

2. What is the index number of the end of the string ?
>>4

3. What is the length of the string?
>>5
4. What is the MAXIMUM value that this expression can return for 'y' ? : y<"Hello".length()
>>5(since "Hello".length() is 5)

5. What is the difference between the values of your answers to points 4 and point 2 above?
>>sorry i did not clearly understand this. point 4 gives answer as 5 and point 2 gives answer as 4 so both lenght and index are different and index is always one less than length

Right well consider this - the word "Hello". The index starts at zero as you say, so this is character 'H'. Then we add 2 to that, so that points at character 'l', then, because 'y' is now 2, and 2 is still less than 5 (the length of the string 'Hello'), we again add 2 to 'y', making it 4, and take the 4th index character, which is 'o', and so we get 'Hlo'. Now, if 2 is added to 'y', it will exceed the string length, and so this brings the loop to a halt.

this explanation is very clear. I was jumping by mistake to next character rather than alternate character whose difference is 2 not1

comparing my solution with your solution as above both looks almost similar except in the increment i wrote i=i+1 and inside for loop i=i+2 (where as yours i=i+2 and i=i+1) . I wonder what is the difference.

I also made my usual mistake
i<str.length()-1

I though i represent like index so it is 1 less than original length.

I think your best bet to figure this out would be to "hand test" by making a table of every assignment change to see where your values are awry...

Think about what you are doing in the loop!

Putting your "return" INSIDE the loop makes NO SENSE! It would STOP looping the first time it hits it! Compare to the OTHER solutions offered that build up the proper answer and only then returns that answer AFTER the loop completes...

You also need to understand the way "++i" "i++" and "i=i++" work... go over these operators in a good C or Java language reference...

Because not understanding my question 4 is, I believe, the reason you are having trouble, so if you can deal with this point I think you will make progress.

Yes, they both work. But your two examples are making the code more difficult to read than it needs to be, because you can make it easier to understand I would say, if you use StringBuilder and follow the example I gave originally.

(It's good that you made the two pieces of code work, but as someone great once said, 'you should make things as simple as possible, and no simpler'.

0

Featured Post

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Introduction
This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…

The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…