|
|
|
Sunday, 20 February 2011 23:59 |
// Reads & reports the number of words that are repeated in the sentence!
public class stutter
{
public static void main (String[] args)
{
String str = " ";
String[] words = str.split (" ");
for (int i=0; i
{
if(words[i].equals(words[i-1])) //Stuttered
System.out.println (words[i]);
}
}
} Read more: |
|
|
Monday, 20 September 2010 11:54 |
sprintf and snprintf where things I used to really missing from C.
Here is a straight forward snprintf implementation.
public static String snprintf( int size, String format, Object ... args ) {
StringWriter writer = new StringWriter( size );
PrintWriter out = new PrintWriter( writer );
out.printf( format, args );
out.close();
return writer.toString();
}
 Read more: |
|
Tuesday, 20 July 2010 23:35 |
Basic sample of hacking inheritance into Javascript as well as demonstrating the usage of getters and setters
Function.prototype.inherits = function(fnParent) {
this.prototype.super = fnParent;
for (var s in fnParent.prototype) {
this.prototype[s] = fnParent.prototype[s];
}
return this;
}
parentClass = function (args) {
this._name = '';
this.__defineGetter__("name", function(){
return this._name;
});
this.__defineSetter__("name", function(val){
this._name = val.replace('t','p');
return;
});
}
childClass = function (args) {
this.super(args); //constructor for super
}.inherits( parentClass );
var test = new childClass( );
test.name = 'test';
alert( test.name ); //returns pest
 Read more: |
|
|
Monday, 28 June 2010 03:45 |
// Simply reverses individual words of a string. Keep it low level, do not use abstraction APIs
/**
* ReverseWordsOfAString simply reverses individual words of a string.
* Keep the order of words intact
*
*/
import java.io.*;
class ReverseWordsOfAString
{
public static final int UNKNOW=0;
public static final int INTERACTIVE=1;
public static final int TEST=10;
public static final int BATCH=20;
public static void main(String[] args)
{
switch ( ReverseWordsOfAString.parseCommandLine( args ) )
{
case ReverseWordsOfAString.INTERACTIVE:
ReverseWordsOfAString.executeInteractive( args );
break;
case ReverseWordsOfAString.TEST:
ReverseWordsOfAString.executeTest( args );
break;
default:
System.out.println("Other functions not yet implemented");
}
}
public static int parseCommandLine(String[] args)
{
if (args[0].equals("--interactive"))
return ReverseWordsOfAString.INTERACTIVE;
if (args[0].trim().equals("--test"))
return ReverseWordsOfAString.TEST;
if (args[0].equals("--batch"))
return ReverseWordsOfAString.BATCH;
return ReverseWordsOfAString.UNKNOW;
}
public static void executeInteractive( String[] args )
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while (str != null)
{
System.out.print("> interactive [Hit CTRL-D to exit] ");
str = in.readLine();
System.out.println(processReverseWordsOfAString(str));
}
} catch (IOException e) {}
}
public static void executeTest( String[] args )
{
long start = System.currentTimeMillis();
assert processReverseWordsOfAString("hello francois").equals("olleh siocnarf");
assert processReverseWordsOfAString("hello francois richard how are you doing").equals("olleh siocnarf drahcir woh era uoy gniod");
long end = System.currentTimeMillis();
System.out.println("Elapsed time: " + (end - start) );
}
public static String processReverseWordsOfAString (String arg)
{
if (arg == null)
{
System.out.println("");
System.exit(1);
}
StringBuffer result = new StringBuffer();
StringBuffer word = new StringBuffer();
for (int i=0; i {
if (arg.charAt(i) != ' ' )
word.append(arg.charAt(i));
if (arg.charAt(i) == ' ' )
{
for (int j=word.length()-1; j>=0; j--)
result.append(word.charAt(j));
result.append(" ");
word = new StringBuffer();
}
if (i == arg.length()-1)
{
for (int j=word.length()-1; j>=0; j--)
result.append(word.charAt(j));
}
}
return result.toString();
}
}
 Read more: |
|
|
|
|
|