A delimiter is a character used to signify a boundary betwee

A delimiter is a character used to signify a boundary between pieces of data. This is useful for many practical computer applications. For example, when you send an email to multiple people, you usually write a semicolon or comma as a delimiter to separate the list of recipients. Another common example is using commas as delimiters in CSV (comma-separated values) files, which are easily imported into a spreadsheet like Excel. Write a method explode(String s, String d) that separates the string s according to the delimiter d. You may assume that the delimiter is exactly one character in length. method should return an array of the “words” between the delimiters. Your method should produce empty strings (“”) when you have delimiters at the beginning or end of s, or when s contains multiple delimiters in a row. Use only the String class methods mentioned at the beginning of this assignment in your solution. In particular, do not use the split method

Solution

Program is written below in java 1.8 and tested. Reach out for any queries, comment if this is the solution.

import java.util.ArrayList;
import java.util.List;

public class Strings {
  
   /**
   * return array of String containing words between delimiter, else returns null and prints \"\"
   * @param s
   * @param d
   * @return String []
   */
  
   public static String[] explode(String s, String d){
      
       String [] words = null;
       List<String> wordList = null;
      
       // check if input string starts with or ends with delimiter, if yes, print \"\" and return.
       if(s.startsWith(d)||s.endsWith(d)){
           System.out.println(\"\");
           return null;
       }else{
           // check if input string contains multiple delimiter in a row, if yes, print \"\" and return.
           if(s.contains(d+d)){
               System.out.println(\"\");
               return null;
           }else{
               //split the string into char array.
               char[] inputChars = s.toCharArray();
               wordList = new ArrayList<String>();
               String word = \"\";
               for(int i=0;i<inputChars.length;i++){
                   //if current char is delimiter, then reached end of word, add it to list and reset temp varible \'word\'
                   if(inputChars[i]==d.charAt(0)){
                       wordList.add(word);
                       word=\"\";
                       continue;
                      
                   }
                   //append char to temp var, to form the word, this will be added to \'wordList\' when delimiter is encountered next.
                   word = word+inputChars[i];
               }
               wordList.add(word);
              
           }
              
       }
       if(wordList!=null && !wordList.isEmpty()){
           //convert List to Array.
           words = new String[wordList.size()];
           words = wordList.toArray(words);
       }
       return words;
   }

   public static void main(String[] args) {
       String[] words = explode(\"abc,bcc,cdc\", \",\");
       if(words!=null){
           for(String s:words){
               System.out.println(s);
           }
       }

   }

}

A delimiter is a character used to signify a boundary between pieces of data. This is useful for many practical computer applications. For example, when you sen
A delimiter is a character used to signify a boundary between pieces of data. This is useful for many practical computer applications. For example, when you sen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site