Solved

alternate strings challenge

Posted on 2014-10-18
22
154 Views
Last Modified: 2014-10-20
Hi,

I was working on below challenge

http://codingbat.com/prob/p165666

I wrote as below

public String stringBits(String str) {
  for(int i=0;i<str.length()-1;i=i++){
  String s="";
  return (s+str.substring(i,i+2));  
  }
  return null;
}

Open in new window

I got output as below

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         



How to improve it and fix it. Please advise
0
Comment
Question by:gudii9
  • 8
  • 8
  • 3
  • +1
22 Comments
 
LVL 16

Accepted Solution

by:
krakatoa earned 167 total points
ID: 40389339
Try this:

public String stringBits(String str) {
  StringBuilder sb = new StringBuilder();
  for (int y=0;y<(str.length());y=y+2){
  sb.append(str.charAt(y));
  }
  return sb.toString();
}

Open in new window

0
 
LVL 20

Assisted Solution

by:n2fc
n2fc earned 166 total points
ID: 40389345
public String stringBits(String str) {
  String s="";
  for(int i=0;i<str.length();i+=2)
      s=s+str.substring(i,i+1);  
  
return s;
}

Open in new window

0
 
LVL 20

Expert Comment

by:n2fc
ID: 40389362
As an alternative:

public String stringBits(String str) {
  String s="";
  for(int i=0; i<str.length(); ++i)
       s+=str.substring(i, ++i);  
  
  return s;
}

Open in new window


See if you can tell why they BOTH work!!
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 7

Author Comment

by:gudii9
ID: 40389522
public String stringBits(String str) {
  for(int i=0;i<str.length()-1;i=i++){
  String s="";
  return (s+str.substring(i,i+2));  
  }
  return null;
}







public String stringBits(String str) {
  String s="";
  for(int i=0;i<str.length();i+=2)
      s=s+str.substring(i,i+1);  
  
return s;
}

Open in new window



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.

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40389526
for (int y=0;y<(str.length());y=y+2){
  sb.append(str.charAt(y));

Is y is not like index (index always starts with 0 so we take str.length()-1) but we are taking y<(str.length())
Please advise
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 40389754
Another mistake, is that i=i++ does not do what you seem to think it does
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40389849
Is y is not like index (index  . . .

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.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40389872
gudii - do me this favour will you - answer these 5 questions, based on the string "Hello". :

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

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

3. What is the length of the string?

4. What is the MAXIMUM value that this expression can return for 'y' ? : y<"Hello".length()  

5. What is the difference between the values of your answers to points 4 and point 2 above?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390034
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390037
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
0
 
LVL 20

Expert Comment

by:n2fc
ID: 40390054
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...
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40390073
You got my point 4 wrong. Look at it again more carefully.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40390574
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40390831
'y' ? : y<"Hello".length()

Is it is ternary operator(looks like that to me with ? and :

It also does not look like that since there is no two separate entities corresponding to condition true and false.

Please advise
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40390852
y<"Hello".length()


IS THE EXPRESSION!!!!!!!!!!!!!


Im am asking you the max value of y !!!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 40391394
I think krakatoa means max value of y for which the expression is true
0
 
LVL 7

Author Comment

by:gudii9
ID: 40392598
oh that is  6
0
 
LVL 84

Expert Comment

by:ozo
ID: 40392611
Is this true?
6<"Hello".length()
0
 
LVL 7

Author Comment

by:gudii9
ID: 40392621
oh i made a mistake. I understand the question now. So the y can go maximum upto 4. It if goes beyond 4 say 5  or 6 etc the condition returns false
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40392815
oh i made a mistake. I understand the question now. So the y can go maximum upto 4. It if goes beyond 4 say 5  or 6 etc the condition returns false

That's it ! y's maximum value can be 4 - the highest number that is less than 5, . And 5, as you know, is the length of the string.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40392825
public String stringBits(String str) {
  String s="";
  for(int i=0;i<str.length();i+=2)
      s=s+str.substring(i,i+1);  
  
return s;
}





public String stringBits(String str) {
  String s="";
  for(int i=0; i<str.length(); ++i)
       s+=str.substring(i, ++i);  
  
  return s;
}

Open in new window


Both of above scenarios working since we are incrementing first then assigning to i in the second scenario which makes it equivalent to fist scenario
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40393334
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 34
Delphi: barcode reading on android platform 1 51
java example issue 5 25
Java 8 to Java 6 8 22
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question