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,520 points
11,224 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 29, 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 do I convert a String to an int in Java?

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9, 2022 in Java by Soham
• 9,700 points
2,582 views
0 votes
1 answer

How do I count the number of occurrences of a char in a String?

My 'idiomatic one-liner' for this is: int count ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,910 points
1,791 views
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, 2019 in Java by Sirajul
• 59,230 points
1,888 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,620 points
927 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, 2019 in Java by Sirajul
• 59,230 points
2,513 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, 2019 in Java by Sirajul
• 59,230 points
3,034 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,190 points
3,845 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,910 points
5,408 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, 2019 in Java by Sirajul
• 59,230 points
1,475 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, 2019 in Java by Karan
• 19,610 points
2,752 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP