0

I have the following assignment that I succeeded, however the code is very inefficient, I would appreciate if someone could show me a more efficient way, perhaps with substring. the assignment:

/**
 * Separates a given string into tokens, which are the "words" that are
 * separated by one or more occurrences of the given separator character.
 * Returns the tokens as an array of String values.
 */
public static String[] tokenize (String str, char separator) {
    // Removes all the occurrences of the separator at the beginning and end of str
    String source = trim(str, separator);
    String[] tokens = new String[charRunCount (source,separator)+1];
    String tmp = ""; // a string in order to take a word, then run over this string
    int j = 0;
    int i = 0;
    while (i < tokens.length) {
        if ( source.charAt (j) != separator ) {
            do {
                tmp += source.charAt (j);
                if ( j >= source.length () - 1 ) {
                    break;
                }
                else { // so that we math the source length
                    j++;
                }
            } while (source.charAt (j) != separator); 
        }
        if ( source.charAt (j) == separator ) {
            j++;
            while (source.charAt (j) == separator) {
                j++;
            }
        }
        tokens[i] = tmp;// taking the token into place
        tmp = ""; //resetting the token so we can begin anew

        i++;
    }
    return tokens;
}

the cahrRunCount function:

    public static int charRunCount(String str, char c){
    char last = 0;
    int counter = 0;
    for (int i = 0; i < str.length(); i++) {
        // whenever a run starts.
        if (last != c && str.charAt(i) == c) {
            counter++;
        }
        last = str.charAt(i);
    }
    return counter;
}

I cannot use import or regex, thank you!

2
  • 1
    If your code works but needs improving, CodeReview might be a better place to post it. Commented Nov 5, 2018 at 15:54
  • sorry, I wasn't aware that such a site existed Commented Nov 5, 2018 at 16:09

2 Answers 2

3

Use String.split method

String[] tokens = str.split(separator)


for(String token:tokens){
//your code goes here
}

Docs here

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

Sign up to request clarification or add additional context in comments.

1 Comment

how would you use it in a for loop for example? because separator is a char
1

If you want, you can use the split method of the String class (just like @Amidala Siva Kumar suggested), like this:

public static String[] tokenize (String str, char separator) {
    String[] tokens = str.split(separator);
    return tokens;
}

Or, if you want to do it using your own split, you may do it like this (an improvement to your code).

public static String[] tokenize (String str, char separator) {
    String sep = ""+separator;
    int max_size = str.length() - str.replace(sep, "").length() +1 ; // Calculate max array size
    String[] temp = new String[max_size];
    int start = 0, index = 0, exact_size = 0;
    int pos = str.indexOf(separator);
    while (pos>=start) {
        if (pos>start){
            temp[index++] = str.substring(start,pos).trim();
            exact_size++;
        }
        start = pos + 1;
        pos = str.indexOf(separator,start); 
    }
    String[] tokens = new String[exact_size];
    System.arraycopy(temp, 0, tokens, 0, exact_size); 
    return tokens;
}

Hope you find it useful.

1 Comment

I wasn't aware of the split function, how would you implement it?

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.