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

Removing the middle 2 characters when string length is even  RSS feed

 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Given a string, return a a string with either the middle character removed, when the original string contains an odd number of characters, or the middle two characters removed when the original string contains an even number of characters. Assume there will be at least one character in the original string. Note that you can do this without an if statement.






this works for a word that has an odd length but how can i make it so it can also remove the 2 middle characters if the word length is even??
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i make it so it can also remove the 2 middle characters if the word length is even??

First detect an even length.
Have you tried with paper and pencil to get the indexes and lengths for the parts of the String that need to be extracted and put together again to build a new String with the middle 2 characters removed?
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hint is important: you can do this without using an if-statement.

This means that the formula for determining whether the length is odd or even will be part of the calculation of how many characters you need to remove. It's actually a pretty clever twist to the solution.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and Welcome to the Ranch!
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, and yes that's the part i'm stuck because with the if i might be able to do it but i'm trying to do it without the if statement.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm trying to do it without the if statement.

How do you plan to make the decisions regarding even or odd length?
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have no idea where to begin
 
Stephan van Hulst
Saloon Keeper
Posts: 7804
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
length of stringstart of leftend of leftstart of rightend of right
1
2
3
4
5
6
70347
8

Fill in the rest of the table and see if you recognize a pattern.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Google is your friend. Look up checking for odd or even with Java remainder operator or check out this article: http://www.javaranch.com/drive/modulo.html
 
Stephan van Hulst
Saloon Keeper
Posts: 7804
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need a modulo operator. In fact, you don't need to determine at all whether the length is even or odd.
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to determine the length. The left and right parts-to-be-kept are equal in length. Now, suppose the original string has length 6, then the left part will be 2 characters long. If the string is 7 chars, we need to keep the first 3 chars. So, we need a function f with f(6) = 2, f(7) = f(8) = 3, et  cetera. Can you think of such a function?
 
Campbell Ritchie
Marshal
Posts: 55682
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

If I were doing this, I would use a StringBuilder object.
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think in this instance, the ternary operator counts as an 'if'? ;)

L.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:You don't need a modulo operator. In fact, you don't need to determine at all whether the length is even or odd.

Ok, I see how you can solve this without using the remainder operator.

Piet Souris wrote:You do not need to determine the length.

Not sure what you mean by that because length() is central to the solution, at least it is with the one I got. Are you saying you don't need length() at all or did you mean to say that you don't need to determine whether the length is odd or even?
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Hasan wrote:Do you think in this instance, the ternary operator counts as an 'if'? ;)

L.

Yes, it counts as an if

It's a bit of a brain-teaser, I must admit.

Hint: First cut off the right side to the correct length, then you can use that piece to measure how much of the left side you need to take.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the method you write is named noMid(), here's some code you can use to test your implementation:
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



All i have is this and this only works for any word that has an even amount of characters. i do apologize i am new to this site and barely learning java. i appreciate all the tips and help.
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you insert some System.out.println() statements so that you can see what your start and end parts look like, you'll see that your end part is always correct but your start part will only be correct when string.length() is even.  So, if your end part is always correct, then you can use its length to measure out how much of the string you need for start.
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Assuming the method you write is named noMid(), here's some code you can use to test your implementation:


This might be a good, albeit in-depth, opportunity to read up on Test-Driven Development (TDD). This way of testing the implementation fits perfectly into that.

L.
 
Liutauras Vilda
Marshal
Posts: 4635
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In such exercise pencil and paper are the best tools.

Alvaroo, write down on a piece of paper:

Write indices above each character. Write strings lengths on the right of each string. Once you workout right hand side string start and end indices, think how you can calculate left hand side string start and end.

All is simpler thank you might think. Just visualise all exercise on a paper.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!