Haseeb Waseem
Haseeb Waseem

Reputation: 71

java replacing char in string

  firstLetter = word.charAt(0);
  lastLetter = word.charAt((word.length()) - 1);
  noFirstLetter = word.substring(1);
  newWord = noFirstLetter + firstLetter;
  if(word.equalsIgnoreCase(newWord))

So im trying to take the first letter of the word and if I take the first letter of the word away and move it to the end it should be equal to the same word. My code here isn't working. For example if the user entered "dresser" if you move the "d" to the end of the word you get the word "dresser" again. That is what im trying to check

Upvotes: 1

Views: 143

Answers (3)

Alp
Alp

Reputation: 3105

I think what you are trying to do is to remove the first character, and then check if rest of the characters are symmetrical around the center of the word.(OK even it is complicated for me)

EG:

dresser => (drop d) => resser => (add d to the right) => resserd (read from right to left and it is dresser again).

after you drop the first letter:

resser (there are even number of letters in the word)

r e s s e r
    |_|
  |_____|
|_________|

since they are symmetrical, you can say that that word would be Palindromic if you move D from left to right (or vice versa).

The whole thing above could be horribly wrong if I misunderstood your question at the first place. But I assumed, your question was NOT a simple substring question.

Cheers.

Upvotes: 4

Scott
Scott

Reputation: 1922

Okay, so I'm presuming that you want to take a string, move it's first index to the end and then reverse the string. If you do this to a word such as 'dresser' then you end up with the same value.

Something like this could work, currently untested:

public StringBuilder reverseWord(String word){
    firstLetter = word.charAt(0); //find the first letter
    noFirstLetter = word.substring(1); //removing the first index
    newWord = noFirstLetter + firstLetter; //move first index to end
    return new StringBuilder(newWord).reverse().toString(); //reverse
}

if(reverseWord("dresser").equals("dresser")){
     //do something
}

Edit: As Jose has pointed out, it is important to check the length of the actual parameter by invoking length() on word.

Upvotes: 3

Anonymous Coward
Anonymous Coward

Reputation: 3200

If you move 'd' to the end of the word "dresser" you get "resserd" which is not equal to "dresser".

If you move 'd' to the end and then reverse the word then they are equal.

So, assuming you consider strings equals even if they are reversed, the code you want would be :

boolean testPass = false;
if ( word.length()==0 )
  testPass = true;
else
{
  firstLetter = word.charAt(0);
  noFirstLetter = word.substring(1);
  newWord = noFirstLetter + firstLetter;
  reversed = new StringBuilder(newWord).reverse().toString()
  if (word.equalsIgnoreCase(newWord) || word.equalsIgnoreCase(reversed))
    testPass = true;
}
if ( testPass )
  // Do something

Take notice of the important check of word having lenght 0. Otherwise word.charAt(0) will throw an exception.

Upvotes: 1

Related Questions