0

Hey guys this is my code for splitting the array first without using any inbuilt functions. It works fine, my question is in the second part.

static String[] split(String ss) {

    String[] a = new String[1];
    String s = "";
    for (int i = 0; i < ss.length(); i++) {
        if (ss.charAt(i) == ' ') {
            s += ", "
        } else {
          s += ss.charAt(i);
        }
        for (int j = 0; j < a.length; j++) {
            a[j] = "[" + s  + "]";
        }
    }
    return a;


}

I need now to count each letter in a word and give it out also without inbuilt functions as split, chartoarray and so on. this is to what i came so far.

 for example String="This is just an example". it should give out 
 This=4
 is=2
 ..

static String[][] LettersCount(String[] array) {

    int count=0;
    String [][] a =new String[array.length][array.length];
     String s= "" + Arrays.toString(array);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                count = 0;
            } else {
                count++;
            }
5
  • 1
    can you use maps??:> Commented May 10, 2018 at 21:57
  • 1
    You can modify this solution to count words instead of characters Commented May 10, 2018 at 21:58
  • ok i will look it up thanks! no without maps Commented May 10, 2018 at 22:01
  • The code you give that supposed to work "fine" doesn't work at all. How can you have an argument named "String s" and 2 lines after having another variable named "s" ? Commented May 10, 2018 at 22:02
  • @Antoniossss has already answered it. Well, look also how bucket sort(algorithms) work. Simply create bucket for each letter and then let them fall in their own bucket. Commented May 10, 2018 at 22:29

2 Answers 2

5

You can use property of character that it has a numerical value after all. Lets use it as index and store the counts in an array. (so we will mimic Map this way)

int[] counter = new int[256] ;// this will hold count of all letters
counter[(int) character]++; // this is how you do the counting
Sign up to request clarification or add additional context in comments.

2 Comments

The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
@PatrickParker true but most standard characters fit within the first 256
0
public class JavaArrayLengthTest {
   public static void main(String[] args) {
String[] testArray = { "A", "B", "C" };
int arrayLength = testArray.length;
System.out.println("The length of the array is: " + arrayLength);
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.