From me, you're going to get part code, part verbal explanation.
If the strings being searched are short, the easiest way to do it would be to compare each character in the string with the letter you're looking for in a loop. Each time there is a match, increment a counter.
If you look at the API for String, you'll see two methods:
String.length() -- gets the length of the string
String.charAt(int) -- returns a specific char from the String
So, what you do is first get the length of the string passed in off the command line (let's call it myString):
int len = myString.length();
This is how long you want the loop to run. If your counter is:
int myCounter = 0;
and your char that was passed in by the user is called myChar then the search loop would look like:
And that chould do it. It's not a complete program, but there's not much left to do.
Originally posted by guru mysore:
Hi All,
Can you tell me how to write a program to count the no. of times a character "a" occurs in the name "guruprasad" or any other string data which could be entered in the command line? I need the code and not the verbal explnation.
I am a beginner in java pl. encourage me.
Regards,
Guru
Send mail to gurumysore1@yahoo.com