Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

to count the no. of times a character occurs in a string  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guru,
Try the following.

Enjoy,
Manfred.
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!