469,579 Members | 1,214 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

using indexOf to count the number of times a letter occurrs?

evilmonkey
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same result. I've started to try this but not sure I am on the right track. any help or push in the right direction would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.StringTokenizer;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Main {
  6.  
  7.  public static void main(String[] args) {
  8.  
  9.  
  10.   System.out.println("Enter the sentence :");
  11.  
  12.   alphaCount alpha =new alphaCount(); 
  13.   Scanner input = new Scanner(System.in);
  14.   String output = "";
  15.   String sentence = input.nextLine();
  16.   StringTokenizer token = new StringTokenizer (sentence );
  17.  
  18.   //String[] words = sentence.split(" "); 
  19.   System.out.println("Number of A's is :"+sentence.indexOf('a'));
  20.   //System.out.println("Number of B's is :"+alpha.numOfBs(sentence));
  21.  }
  22.  
  23. }
  24.  
  25. public class alphaCount {
  26.  
  27.  
  28.     /** Creates a new instance of alphaCount */
  29.     public alphaCount() {
  30.     }
  31.  
  32.   // I could brute force each letter of the alphabet like this 
  33.  public static int numOfAs(String str) {
  34.  int numA = 0;
  35.  
  36.   for(int i = 0;i < str.length();i++) {
  37.    if(str.charAt(i) == 'a') {
  38.     numA++;
  39.    }
  40.  
  41.   }
  42.   return numA;
  43.  }
  44.  
  45.  public static int numOfBs(String str) {
  46.   int numB =0;
  47.  
  48.   for(int i = 0;i < str.length();i++) {
  49.    if(str.charAt(i) == 'b') {
  50.     numB++;
  51.    }
  52.  
  53.   }
  54.   return numB;
  55.  }
  56.  
  57.  
  58.     }
Feb 21 '07 #1
4 13505
Ganon11
3,652 Expert 2GB
You could define a method, numOfChars, that takes a character as its parameter and returns the amount of that character in the string. Thus, you could use sentence.numOfChars('a'); to find the number of a's, sentence.numOfChars('G'); for G's, and so on.
Feb 21 '07 #2
r035198x
13,262 8TB
Is there a way to use index of in java to pull the number of occurrences of every letter in a user defined sentence? I can brute force this but is there a faster more elegant way to achieve the same result. I've started to try this but not sure I am on the right track. any help or push in the right direction would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. import java.util.StringTokenizer;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8.  
  9.  
  10. System.out.println("Enter the sentence :");
  11.  
  12. alphaCount alpha =new alphaCount(); 
  13. Scanner input = new Scanner(System.in);
  14. String output = "";
  15. String sentence = input.nextLine();
  16. StringTokenizer token = new StringTokenizer (sentence );
  17.  
  18. //String[] words = sentence.split(" "); 
  19. System.out.println("Number of A's is :"+sentence.indexOf('a'));
  20. //System.out.println("Number of B's is :"+alpha.numOfBs(sentence));
  21. }
  22.  
  23. }
  24.  
  25. public class alphaCount {
  26.  
  27.  
  28. /** Creates a new instance of alphaCount */
  29. public alphaCount() {
  30. }
  31.  
  32. // I could brute force each letter of the alphabet like this 
  33. public static int numOfAs(String str) {
  34. int numA = 0;
  35.  
  36. for(int i = 0;i < str.length();i++) {
  37. if(str.charAt(i) == 'a') {
  38.     numA++;
  39. }
  40.  
  41. }
  42. return numA;
  43. }
  44.  
  45. public static int numOfBs(String str) {
  46. int numB =0;
  47.  
  48. for(int i = 0;i < str.length();i++) {
  49. if(str.charAt(i) == 'b') {
  50.     numB++;
  51. }
  52.  
  53. }
  54. return numB;
  55. }
  56.  
  57.  
  58. }
Might want to have a read at regular expressions as well.
Feb 22 '07 #3
bloid
1
Did you mean something like:
Expand|Select|Wrap|Line Numbers
  1. String str = "Hello there!!" ;
  2. char ch = 'e' ;
  3. int count = 0 ;
  4. int pos = -1 ;
  5. while( ( pos = str.indexOf( ch, pos + 1 ) ) > -1 ) count++ ;
  6. System.out.println( "The character " + ch + " occurs " + count + " times" ) ;
?
Feb 22 '07 #4
Did you mean something like:
Expand|Select|Wrap|Line Numbers
  1. String str = "Hello there!!" ;
  2. char ch = 'e' ;
  3. int count = 0 ;
  4. int pos = -1 ;
  5. while( ( pos = str.indexOf( ch, pos + 1 ) ) > -1 ) count++ ;
  6. System.out.println( "The character " + ch + " occurs " + count + " times" ) ;
?
Yes I did, but I actually figured it out and it is working, that was close to what I got.
thank you for the reply
Feb 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by jester.dev | last post: by
4 posts views Thread by Japhy | last post: by
1 post views Thread by Daveyk0 | last post: by
1 post views Thread by ratnakarp | last post: by
4 posts views Thread by onecorp | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.