LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2006, 10:14 PM   #1
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Rep: Reputation: 30
Counting number of occurences of a certain chacter in string java


for example "stsigijfgigdfksssdoggog" I want my program to be able to tell me how many s are there in that string.
 
Old 04-27-2006, 10:44 PM   #2
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
int vals[255];
int i;
for(i=0; i < 255; i++)
vals[i] = 0;

char str[100];
strcpy(str, "stsigijfgigdfksssdoggog");

for(i=0; i<strlen(str); i++)
vals[str[i]]++;

printf("s occurred %ld times", vals[115]); // ASCII 115 is 's'
 
Old 04-27-2006, 10:53 PM   #3
jdwilder
Member
 
Registered: Jul 2003
Location: United States
Distribution: Fedora Core 7 and older, Knoppix, Ubuntu
Posts: 121

Rep: Reputation: 15
Off the top of my head someone could do

String s=new String("stsigijfgigdfksssdoggog");
int counter;
for (int i=0;i<s.length();i++){
if (s.charAt(i)=="s")coutner++;
}

but the probably is a more efficient way. s[i] should work instead of s.charAt(i) and may be more efficient, but it really isnt the object oriented way of doing things. I also do not remember if denoting a string is ' or ".
 
Old 04-27-2006, 10:57 PM   #4
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Oh, I thought this was for C not java. Sorry. jdwilder's solution would work too, but I believe it would be:

String s=new String("stsigijfgigdfksssdoggog");
int counter = 0; // better to be explicit
for (int i=0;i<s.length();i++){
if (s.charAt(i)=='s')counter++;
}
 
Old 04-27-2006, 11:10 PM   #5
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Original Poster
Rep: Reputation: 30
thanks guys
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
counting number of sheets printed from a printe avirup dasgupta Linux - General 2 07-25-2005 07:36 PM
Problems in counting ROUTERS using JAVA nedian123 Linux - Networking 0 07-08-2004 06:59 AM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 07:12 AM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration