DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99

    Culture Shock implementation

    Here's my implementation. Comments?

    Code:
    public class CultureShock {
    	
    	public static String translate (String input) {
    		if(input == null || input.length() < 3) {
    			return input;
    		}
    		StringBuffer sb = new StringBuffer(input);
    		
    		char prev = ' ';
    		int size = sb.length();
    		for(int index = 0; index < size; ++index) {
    			char cur = sb.charAt(index);
    			if(prev == ' ' &&  size - index > 2) {
    				if(cur == 'Z') {
    					cur = sb.charAt(++index);
    					if(cur == 'E') {
    						cur = sb.charAt(++index);
    						if(cur == 'E') {
    							if(index == size -1 || sb.charAt(index + 1) == ' ') {
    								sb.setCharAt(index,'D');
    							}
    						}
    					}
    				}
    			}
    			prev = cur;
    		}
    		return sb.toString();
    	}
    
    	public static void main(String[] args) {
    		String var;
    		if(args.length == 1) {
    			var = args[0];
    			System.out.println("In: '"+var+"' Result: '" + CultureShock.translate(var) + "'");
    		}
    		var = "";
    		System.out.println("In: '"+var+"' Result: '" + CultureShock.translate(var) + "'");
    		var = "THE TWENTY SIXTH LETTER OF THE ALPHABET IS ZEE";
    		System.out.println("In: '"+var+"' Result: '" + CultureShock.translate(var) + "'");
    		var = "ZEE";
    		System.out.println("In: '"+var+"' Result: '" + CultureShock.translate(var) + "'");
    		var = "ZEE ZEE ZED ZEDS ZEE ZEES";
    		System.out.println("In: '"+var+"' Result: '" + CultureShock.translate(var) + "'");
    	}
    }

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    Looks good - rather than going through a character at a time you could use

    Code:
     int 	string.lastIndexOf(String str);

    It also looks like from version 1.4.2 there is also a replaceAll which makes stuff even easier

    Code:
     String 	string.replaceAll(String regex, String replacement)

    But a very good and clear implementation

    Graham

  3. #3
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Ah, true. I'm using 1.3.1

    Thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links