How do we convert only the second letter of every word in a string to uppercase ?

0 votes

Is there any built-in function that capitalizes only a chosen letter of our choice, if not what is the code for it ?

For ex:

I want only the 2nd letter of every word to be capitalzed.

i love  ice creAMS ----> i lOve  iCe cReams 

Aug 28, 2018 in Java by bug_seeker
• 15,310 points
38 views

2 answers to this question.

+2 votes
Best answer
The following code will perform your desired action:

public static void main(String[] args)

{

//Accept String

System.out.println("Enter you text here");

Scanner in= new Scanner(System.in);

String string=in.nextLine();

//Convert entire string to lower case

char ch[]=string.toLowerCase().toCharArray();

//second letter of every word is capitalised

for(int i=0;i<ch.length;i++)

{

ch[0]=Character.toUpperCase(ch[0]);

if (Character.isWhitespace(ch[i]) && Character.isLetter(ch[i+1]))

ch[i+2]=Character.toUpperCase(ch[i+2]);

}

String str= new String(ch);

System.out.println(str);

}
answered Aug 28, 2018 by curious
• 560 points

selected Sep 4, 2018 by Omkar
+1 vote
String string = "i love  ice creAMS";
        
        String[] str = string.toLowerCase().split(" ");
        
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < str.length; i++) {
            char[] subCh = str[i].toCharArray();
            
            for (int j = 1; j < subCh.length; j++) {
                if (Character.isLetter(subCh[j])) {
                    subCh[j] = Character.toUpperCase(subCh[j]);
                    break;
                }
            }
            sb.append(subCh);
            sb.append(" ");
            
        }
        String upgString = sb.toString().trim();
        
        System.out.println(upgString);
answered Sep 4, 2018 by Ram Vadlamudi
Hi.. Can you please explain what this for loop does in your code? I am confused.

for (int j = 1; j < subCh.length; j++) {
                if (Character.isLetter(subCh[j])) {
                    subCh[j] = Character.toUpperCase(subCh[j]);
                    break;
                }

Related Questions In Java

0 votes
1 answer

How can I convert a String variable to a primitive int in Java

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
85 views
0 votes
1 answer

How do I create a Java string from the contents of a file?

If you're looking for an alternative that ...READ MORE

answered Apr 19, 2018 in Java by Rishabh
• 3,540 points
72 views
0 votes
1 answer

How can I invoke a method when the method name is in the form of a given string?

I tried doing it this way - java.lang.reflect.Method ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,280 points
42 views
0 votes
1 answer

How to convert a JSON String into Object in Java?

We can easily convert a JSON String ...READ MORE

answered Jun 8, 2018 in Java by parth295
• 4,600 points
219 views
0 votes
1 answer

Can we convert a string to hexadecimal in java

I find this to be an easy ...READ MORE

answered Jul 4, 2018 in Java by Akrati
• 3,150 points
93 views
0 votes
2 answers

How do we convert JSON to Map in Java

When you don't know structure of json. ...READ MORE

answered Oct 31, 2018 in Java by Sushmita
• 6,840 points
129 views
0 votes
1 answer

What is the easiest way to iterate through the characters of a string in Java?

The below code seems the easiest to ...READ MORE

answered Jul 13, 2018 in Java by scarlett
• 1,280 points
40 views
0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,320 points
40 views
0 votes
1 answer

How to convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) { ...READ MORE

answered Sep 26, 2018 in Java by sharth
• 3,320 points
122 views
0 votes
1 answer

How to check if a string has only letters

The following is the code for your ...READ MORE

answered Aug 29, 2018 in Java by curious
• 560 points
25 views