Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Removing the middle 2 characters when string length is even

Greenhorn
Posts: 24
• 1

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??

Rancher
Posts: 2968
32

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?

Sheriff
Posts: 11828
193
• 2
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: 11828
193
• 1
...and Welcome to the Ranch!

Alvaroo Hernandez
Greenhorn
Posts: 24
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.

Rancher
Posts: 2968
32

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
i have no idea where to begin

Saloon Keeper
Posts: 8734
162
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: 11828
193

Stephan van Hulst
Saloon Keeper
Posts: 8734
162
You don't need a modulo operator. In fact, you don't need to determine at all whether the length is even or odd.

Master Rancher
Posts: 2550
87
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?

Marshal
Posts: 58830
179
Welcome again

If I were doing this, I would use a StringBuilder object.

Greenhorn
Posts: 24
4
Do you think in this instance, the ternary operator counts as an 'if'? ;)

L.

Junilu Lacar
Sheriff
Posts: 11828
193

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: 11828
193

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: 11828
193
Assuming the method you write is named noMid(), here's some code you can use to test your implementation:

Alvaroo Hernandez
Greenhorn
Posts: 24

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: 11828
193
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

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.

Marshal
Posts: 5635
390
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.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.