DEV Community

Cover image for converting default method to program in string
Neelakandan R
Neelakandan R

Posted on

converting default method to program in string

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));
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

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));
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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);

        }
    }

}

Enter fullscreen mode Exit fullscreen mode

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);
        }
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:India Won Newland

5.Removing Unwanted space:

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);

        }
    }

}


Enter fullscreen mode Exit fullscreen mode

Output:India

6.Removing space from front

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);

        }
    }

}


Enter fullscreen mode Exit fullscreen mode

Output:India123

7.Removing unwanted space inbetweenand first unwanted space

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
                }
            }

        }
    }

}


Enter fullscreen mode Exit fullscreen mode

Output:mahendra singh dhoni

Top comments (0)