Help needed for writing a code
Write a function trim() that will take an array of characters as an input and remove multiple consecutive spaces from the array (You cannot use the String class in java)..
Can anyone help me out?
i dont know how to use trim() method in a char array[]..
Thanks..
Re: Help needed for writing a code
What have you tried? Where are you stuck?
You're supposed to *write* the trim() method, not use a preexisting one.
Re: Help needed for writing a code
^Yes i tried.. I have took an array of characters.. But im not understanding how to remove the multiple spaces from that array.. Can you please explain how to do that?
Re: Help needed for writing a code
Can you please explain what you've tried and where you're stuck? It's best to post an SSCCE that demonstrates what you've done.
Re: Help needed for writing a code
Here's an idea: Take a piece of paper and pen and draw some slots representing an array with some letters and some empty spaces. Represent array indexes by putting arrows under the slots in the array.
Then look at finding the first slot on the left where there are multiple spaces and scan over that to the first slot where there is a letter. Now think about how to move the letters and change the indexes to move the letters left to cover the spaces.
Re: Help needed for writing a code
Quote:
Originally Posted by
Norm
Here's an idea: Take a piece of paper and pen and draw some slots representing an array with some letters and some empty spaces. Represent array indexes by putting arrows under the slots in the array.
Then look at finding the first slot on the left where there are multiple spaces and scan over that to the first slot where there is a letter. Now think about how to move the letters and change the indexes to move the letters left to cover the spaces.
thanks.. I think i got what you have tried to told.. Will let you know if i again find any further problem..
Re: Help needed for writing a code
@kevin I tried till this point..
Quote:
public class TrimTest{
public static void main(String[]args){
char[] data={some characters here including spaces};
for(int c=0;c<data.length;c++){
System.out.print(data[c];
}
got stuck there... I thought there is a way apply trim() method to the array to remove the spaces like String..
Re: Help needed for writing a code
Please use code tags not quote tags around your code.
Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Help needed for writing a code
Code :
/** Trims the character array from the given character
* Since you can't shorten the array it'll just fill the remaining end of the array with spaces
*
* @param c The to-be-trimmed array
* @param r The to-be-trimmed character(in your case space)
**/
public static void trim(char[] c, char r)
{
for(int i = 0; i<c.length; c++)
if(c[i]==r){
for(int i2 = i+1; i2<c.length; i2++)
c[i-1] = c[i];
c[i] = ' ';
}
}
and if you want to call this function in the main:
Code :
public static void main(String[]args)
{
char[] data={some characters here including spaces};
trim(data, ' ');
for(int c=0;c<data.length;c++){
System.out.print(data[c];
}
}
Re: Help needed for writing a code