Monday, October 14, 2013

String reverse,trim() without predefined methods in java

Reverse :

public class StringReverse {
public static void main(String[] args) {
String str = "kavuri";
char[] s = new char[str.length()];
for (int j = 0, i = (str.length() - 1); i >= 0; i--) {
s[j++] = str.charAt(i);
}
System.out.println("the given string is--" + str);
System.out.println(s);
}
}


trim() without method :

public class RemoveSpaces {

public static void main(String[] args) {
String s = "      welcome   obama      ";
String s1 = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
continue;
} else {
s1 += s.charAt(i);
}
}
System.out.println(s1);
}
}


No comments:

Post a Comment