i want a code for this defition

  • Thread starter tan.like.friends
  • Start date
T

tan.like.friends

write a java program to remove a particular character from a string
that is given by user.
 
W

Wildemar Wildenburger

M

Mark Space

Knute said:
$20. I take credit cards.

$20? You're cheap. I'd charge $50. At least. Depends on how much
testing and how important efficiency is. Always get the full spec.
Pulling characters out of really big string could be hairy if you want
something better than O(N^2).

And credit cards can easily be reversed. I'd insist on a cashier's check.
 
L

Lew

Mark said:
$20? You're cheap. I'd charge $50. At least. Depends on how much
testing and how important efficiency is. Always get the full spec.
Pulling characters out of really big string could be hairy if you want
something better than O(N^2).

And credit cards can easily be reversed. I'd insist on a cashier's check.

Yeah, but then you could give the poster a low "star" rating.

To the OP:

Since it's a homework problem about String, the first thing to do is to check
out the API docs for String, and see if something in there could serve in your
algorithm.
<http://java.sun.com/javase/6/docs/api/java/lang/String.html>

Did you think about the algorithm? What have you got for it so far?
 
J

Joshua Cranmer

Wildemar said:
System.out.printl("Do your own homework.");
System.out.println("Do you own homework.");

Do I get a $2.56 check for finding a bug?
 
W

Wildemar Wildenburger

Joshua said:
System.out.println("Do you own homework.");
-> "Do you¡r! own homework."
Do I get a $2.56 check for finding a bug?
Certainly. I'll send the check as soon as my $2.56 grammar correction
fee comes in.

/W
 
J

Jeff Higgins

tan.like.friends said:
write a java program to remove a particular character from a string
that is given by user.
OK

public class RCO
{
public static void main(String[] args)
throws Exception
{
if(args.length == 0)
{
System.out.println(
"Usage: java RCO sed [OPTIONS] " +
"{script-only-if-no-other-script} " +
"[input-file]");
System.out.println("Example: " +
"java RCO sed -i 's/e//g' 'test'");
}
else
{
Runtime.getRuntime().exec(args);
}
}
}

or
<http://mindprod.com/jgloss/gettingstarted.html>
and
<http://java.sun.com/docs/books/tutorial/>
or
<http://www.rentacoder.com/>
 
M

Mark Space

Lew said:
Yeah, but then you could give the poster a low "star" rating.

You're right. I give the do-my-homework-for-me guy one star: *

And there it is.
 
N

nebulous99

Pulling characters out of really big string could be hairy if you want
something better than O(N^2).

Really? The only naive algo I can think of that's O(N^2) is more-or-
less Java-specific, and that's to keep reading a character, checking
if it's the one to omit, and if not:

String s;
....
s = s + c;

which implicitly keeps copying the whole portion of the string built
so far for every added character.

The obvious, making s a StringBuffer or Builder and using .append(),
avoids this problem, though if you don't preallocate the buffer to the
size of the input string s it will get doubled in size involving a
copy now and again resulting in N log N performance. With
preallocation, it's strictly O(N). The naive C algorithm for doing the
same job mallocs a char array of the size of the input string and
advances pointers copying characters and is O(N).

(The final copy of a StringBuffer/Builder to a String in Java is also
O(N).)

So while the naive Java code for this is O(N^2) it's hardly "hairy" to
make it O(N); it doesn't require using non-java.lang API for heaven's
sake, and even the naive code for this task is typically O(N) in
languages whose "usual" string type is mutable.
 
N

nebulous99

The obvious, making s a StringBuffer or Builder and using .append(),
avoids this problem, though if you don't preallocate the buffer to the
size of the input string s it will get doubled in size involving a
copy now and again resulting in N log N performance.

Bah -- most doubling-related algorithms cause a log N factor to appear
but this one looks like it does not. Adding up the amount copied in
toto gives you 2N - 1, *plus* log N for the overhead on each doubling,
if the allocation of a block of memory is O(1) as it should be in a
decent JVM. 2N + log N - 1 is O(N). It's still inefficient though,
about half as fast as with preallocation. Preallocation actually
wastes less memory on average too -- the worst case (every character
is omitted) wastes a full N bytes plus overhead, while the worst case
for reallocation has it double adding the final character that will be
added when no characters are omitted resulting in wasting at least N
bytes -- the next power of 2 higher than N minus one bytes I think.
The best case scenarios are the same, zero bytes wasted. Reallocation
also churns objects under the hood, while preallocation does not,
leaving more work for the GC at some point. So preallocation is still
best; use the underappreciated int-accepting constructor of
StringBuffer/Builder (both have this constructor).
 
K

kaldrenon

write a java program to remove a particular character from a string
that is given by user.

$string = s/$_//g;

YOU figure out how to make it a Java program.
 
J

Joshua Cranmer

Wildemar said:
-> "Do you¡r! own homework."

Certainly. I'll send the check as soon as my $2.56 grammar correction
fee comes in.

/W

I knew I should have copy/pasted...

BTW, I just sent you ZWD$2.56 yesterday...
 
M

Martin Gregorie

Ian said:
By the time he gets it, it will be worth ZWD$0.26 :-(
>
It will still be worth ZWD$2.56 ....or about $0.001 in any other
dollar-using nation's currency.
 
J

Joshua Cranmer

Martin said:
It will still be worth ZWD$2.56 ....or about $0.001 in any other
dollar-using nation's currency.

The official exchange rate is ZWD$30,000 = $1 as of 9/6. Before that, I
believe it was about ZWD$250 = $1.

The black market rate is closer to about ZWD$600,000 = $1.

I remember reading once that a Zimbabwean was fretting because his
monthly salary was only ZWD$5M and the monthly rent was ZWD$2M. Well, at
least it's easy to be a millionaire in Zimbabwe...
 
L

Lew

Joshua said:
The official exchange rate is ZWD$30,000 = $1 as of 9/6. Before that, I
believe it was about ZWD$250 = $1.

The black market rate is closer to about ZWD$600,000 = $1.

I remember reading once that a Zimbabwean was fretting because his
monthly salary was only ZWD$5M and the monthly rent was ZWD$2M. Well, at
least it's easy to be a millionaire in Zimbabwe...

Actually, you don't make it sound easy at all. Easy to become one, surely,
but not to be one.
 
R

Roedy Green

write a java program to remove a particular character from a string
that is given by user.

Perhaps for you English is a second language. When you issue a bald
command like that, English-speaking people feel obligated to get angry
with you.

Ways of making the request polite include.

"I am looking for a Java program that..."

"Does anyone know how to write a program that.."

"Please, could someone write me a program that..."

English requires these circumlocutions. When you leave them out, it
suggests you hold yourself in very high esteem and the person you are
asking the favour of to be your slave, with no choice in whether he
complies.

This is the way a two year old speaks when he is just learning the
language, and the limits of his power to control others.

As a toddler, I recall ordering my parents to "go to meeting" when I
was frustrated by their meddling interference. They just burst out
laughing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top