• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Compressor  RSS feed

 
Jon Moore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a lab that asks us to compress a single word string that will be entered by the user (the professor).
If you enter aabbbbbaaaa = 2a5b4a would be printed. My main problem functionally is that I am having trouble finding a run of letters.

I have converted the string entered into a character array. I then cycle through the array with a for loop. what I was trying to do was a if statement with (word[i] == word[i+1]) this throws a out of bounds exception with the array so I did (word.length-1) in the for loop. this will not compare the last letter though. The purpose of the If statement was to increment a counter. My thought process was after I had the count I would try to concat this with the original string that was entered. I am not really sure how to solve this problem. Thanks for any help in advance.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a standard homework problem, so you'll not get any solutions... however... if you post the code that you have so far, we can probably help you with the out of bounds issue.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this throws a out of bounds exception with the array so I did (word.length-1) in the for loop


You do know that word.length-1 is the last index, since array indexes are zero based -- so with word.length-1, as the last of your for loop, and i+1, in your if statement, you can access pass the array.

Henry
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have got half the solution already. You are correct about length-1, and concatenating. But you are far better to use a StringBuilder for concatenating several times.

You are also right about incrementing a counter when you have two letters the same.

Then when you have finished, you simply have the last letter to add.

I shan't say any more, because it would spoil it for you to be given an answer.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now look at Henry's hint: If you go through the loop i - 1 times (ie 10 times for the String you quoted), and you end up on the last letter . . .

Which letter are you actually missing?

Also: have a look at the conventional syntax for a for loop, and remember how you were taught to use it for an array.
 
Jon Moore
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice. I am going to give StringBuilder a try.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!