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
• 14,970 points
30 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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,310 points
45 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,520 points
60 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,220 points
31 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,590 points
163 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
50 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,820 points
105 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
28 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,310 points
35 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,310 points
92 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
21 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.