1.small to capital
package String_start; public class small_caps { public static void main(String[] args) { String name = "neelakandan"; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); System.out.print((char) (ch - 32)); } } }
Output:NEELAKANDAN
package String_start; public class capi_small { public static void main(String[] args) { String name = "NEELAKANDAN"; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); System.out.print((char) (ch + 32)); } } }
Output:neelakandan
2.small to capital without changing number
package String_start; public class small_caps { public static void main(String[] args) { String name = "neelakandan123"; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if(ch>='a' && ch<='z') System.out.print((char) (ch - 32)); else System.out.print(ch); } } }
Output:NEELAKANDAN123
3.cap_small_inbetween
package String_start; public class cap_small_inbetween { public static void main(String[] args) { String name = "NEELAKANDAN"; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (i % 2 != 0) System.out.print((char) (ch + 32)); else System.out.print(ch); } } }
Output:NeElAkAnDaN
4.spacebefore_cap_without no1
package String_start; public class spacebefore_cap_no1 { public static void main(String[] args) { String name = "IndiaWonNewland"; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (ch >= 'A' && ch <= 'Z' && i != 0)// i!=0 no space between first System.out.print(" " + ch); else System.out.print(ch); } } }
Output:India Won Newland
5.Removing Unwanted space back:default medhod->name.stripTrailing();
package String_start; public class Removing_unwan_space { public static void main(String[] args) { String name = "India "; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (ch >= 'A' && ch <= 'Z'||ch >= 'a' && ch <= 'z' ) System.out.print(ch); } } }
Output:India
6.Removing space from front//default medhod--->name.stripleading();
package String_start; public class Removing_unwan_space { public static void main(String[] args) { String name = " India123"; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (ch != ' ')// i!=0 no space between first System.out.print(ch); } } }
Output:India123
7.Removing unwanted space inbetweenand first unwanted space//default method--name.trim()--->unwanted front back
package String_start;
public class remove_space_inbetween { public static void main(String[] args) { String name = " mahendra singh dhoni"; boolean frist = false;//remove space from first for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (ch != ' ') // print letter { System.out.print(ch); frist = true;// check next } else { if (frist == true)// frist space only { System.out.print(ch); frist = false;// stop with one space } } } } }
Output:mahendra singh dhoni
Top comments (0)