public class Lab6 { public static void main(String argv[]) { String str; //1) str = "Hello world"; ST stok= new ST(str); System.out.println(str); while (stok.hasMoreTokens()) { System.out.println("#tokens = " + stok.countTokens()); System.out.println("token: " + stok.nextToken()); } System.out.println("#tokens = " + stok.countTokens()); System.out.println("\n\n"); //2) str = " Hello world "; stok= new ST(str); System.out.println(str); while (stok.hasMoreTokens()) { System.out.println("#tokens = " + stok.countTokens()); System.out.println("token: " + stok.nextToken()); } System.out.println("#tokens = " + stok.countTokens()); System.out.println("\n\n"); //3) // str = "root:x:0:0:root:/root:/bin/bash"; // stok = new ST(str, ":"); // System.out.println(str); // System.out.println("username = " + stok.nextToken()); // System.out.println("password = " + stok.nextToken()); // System.out.println("userid = " + stok.nextToken()); // System.out.println("groupid = " + stok.nextToken()); // System.out.println("comment = " + stok.nextToken()); // System.out.println("home dir = " + stok.nextToken()); // System.out.println("shell = " + stok.nextToken()); // System.out.println("\n\n"); //4) // str = "Hello-world.It is!a nice day,today"; // stok= new ST(str,"-.!, "); // System.out.println(str); // while (stok.hasMoreTokens()) // { // System.out.println("#tokens = " + stok.countTokens()); // System.out.println("token: " + stok.nextToken()); // } // System.out.println("#tokens = " + stok.countTokens()); } } class ST { String star[]; int numtokens = 0; int index=0; public ST(String s, String d) { star = new String[50]; String str = s; int start = 0; int end =0; int u = str.indexOf(' '); String c0; for(int i=0; i<str.length();> { c0 = str.substring(i,i+2); System.out.println("[" + c0 +"]"); String c1 = c0.substring(0,1); String c2 = c0.substring(1,2); System.out.println("[" + c1 + "]"); System.out.println("[" + c2 + "]"); if(c1 == " ") { if(c2 != " ") { start = i+2; } } if(c1 != " ") { if(c2 == " ") { end = i; } } if (end < start) { end = str.length(); } star[numtokens] = str.substring(start,end); System.out.println("[" + star[numtokens] + "]"); numtokens ++; } } public ST(String s) { this(s, " "); } public int countTokens() { return(numtokens - index); } public boolean hasMoreTokens() { return (index < numtokens); } public String nextToken() { return star[index++]; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)