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,360 points
64 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
2 answers

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

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 39,420 points
190 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
108 views
0 votes
2 answers

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

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19 in Java by Sirajul
• 39,420 points
72 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21 in Java by Sirajul
• 39,420 points
411 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
416 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,880 points
274 views
0 votes
2 answers

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

There are two approaches to this: for(int i ...READ MORE

answered Aug 19 in Java by Sirajul
• 39,420 points
91 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,350 points
53 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,350 points
210 views
0 votes
2 answers

How to check if a string has only letters

You could probably use Java 8 lambda ...READ MORE

answered Aug 30 in Java by Karan
• 6,000 points
46 views