8 Replies - 4646 Views - Last Post: 11 July 2011 - 08:23 AM Rate Topic: -----

#1 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

method: rotate a string

Posted 10 July 2011 - 10:17 PM

This program is supposed to include a method String stringRotation(String stringOfLetters, int n) that rotates a String n characters to the right. For example, if the inputs are hellothere and 2 then the new string is rehellothe.

I'm having a little trouble with my stringRotation method. The error is : cannot find symbol method append char.And is there something wrong logically with this section of the code? I feel like there is a simpler way to do this, and I am making it more complicated than it has to be. Would it be better to use .replace() ?

This post has been edited by lolitacharm: 11 July 2011 - 07:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: method: rotate a string

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: method: rotate a string

Posted 10 July 2011 - 10:23 PM

Create a char array of the right length, then loop the String using charAt placing those chars in the proper position in the char array and then use new String(char[]) to create your String.
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: method: rotate a string

Posted 11 July 2011 - 07:25 AM

You get that error because in Java Strings are immutable and don't have an append method. You probably meant to use a StringBuilder, which does have an append method.

That said the simplest way to solve this problem would be to simply use String's substring method to get both the substring from 0 to length-n and the substring containing the last n characters and then just concatenate them together with +.

This post has been edited by sepp2k: 11 July 2011 - 07:28 AM

Was This Post Helpful? 3
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: method: rotate a string

Posted 11 July 2011 - 07:32 AM

masijade's solution is more memory efficient, b/c the substring() method returns a new String object each time it is invoked. So the substring() solution is O(n) in terms of memory, and masijade's solution is O(k).
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: method: rotate a string

Posted 11 July 2011 - 08:06 AM

View Postmacosxnerd101, on 11 July 2011 - 04:32 PM, said:

masijade's solution is more memory efficient, b/c the substring() method returns a new String object each time it is invoked. So the substring() solution is O(n) in terms of memory, and masijade's solution is O(k).


The first thing his solution does is to create an array of size n, so I don't see how his memory consumption can be anything but O(n).

Also since the result string has size n, any implementation of this method will have to have a memory consumption of at least O(n) anyway because it has to create the string at the end.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: method: rotate a string

Posted 11 July 2011 - 08:08 AM

His only creates one array though, and only creates one additional String. So that's constant- two objects. Yours creates O(n) Strings worst case, where n is the size of the original String, plus the one String object at the end.
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2133
  • View blog
  • Posts: 3,269
  • Joined: 21-June 11

Re: method: rotate a string

Posted 11 July 2011 - 08:21 AM

View Postmacosxnerd101, on 11 July 2011 - 05:08 PM, said:

His only creates one array though, and only creates one additional String. So that's constant - two objects.


It creates one array of size n and one string of size n. That's a memory consumption of 2*n bytes (plus object overhead, I suppose) and thus in O(n).

Quote

Yours creates O(n) Strings worst case, where n is the size of the original String, plus the one String object at the end.


No, it doesn't. It creates two strings via substring, one of size (n-k) and one of size k, plus the result string. That's also a total consumption of 2n bytes (though somewhat more overhead because we have three objects instead of 2, but I dare say that difference is negligible).
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: method: rotate a string

Posted 11 July 2011 - 08:23 AM

Maybe I misunderstood your solution. I thought you were creating substrings in a loop. Maybe I misread. Sorry for the confusion. :)
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,396
  • Joined: 19-March 11

Re: method: rotate a string

Posted 11 July 2011 - 08:23 AM

Gentlemen, two points.

1) Masijade's solution is a good one for this particular application, and worth examining as a special-case solution, but the StringBuilder approach is more generally applicable and probably a more useful lesson.

2) I'd suggest that performance considerations are far from significant in this application. Unless the original poster is intending to rotate novel-length strings on a production basis, they'll never see a difference between the two versions, and this is both quibbling and premature optimization.

@OP: You should develop a reflex of using a StringBuilder instead of a String any time you're constructing or modifying a String. The idiom would be something like:

public String manipulateString(String s, ...)
{
  StringBuilder sb = new StringBuilder(s);
  // do your thing
  return sb.toString();
}


As a matter of developer time, the StringBuilder is just as easy to work with, and if you develop this habit you'll never have to think about which to use. In an interactive context, it's never likely to make a difference (user response time swamps processor time, and you're dealing with one String at a time) but if it ever does make a difference you'll be using the right one.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1