Home > database >  How to use a loop to capitalize first occurring lowercase letter of string Java
How to use a loop to capitalize first occurring lowercase letter of string Java

Time:10-25

Prompt: Use a loop to find the first occurring lowercase letter in the input string. Then capitalize ONLY the lowercase letter you found, and then re-combine it with the rest of the string.

I'm so confused because one I don't know the exact number of indexes because it varies and two how can I capitalize only the first occurring lowercase. For example inputs are like BYus where I'm only supposed to capitalize the u. I have

public class PasswordImprover {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String userInput;
        userInput = sc.next();
        int userLowerCase = userInput.indexOf(".*[a-z].*");

        for (int i = 0; i < userInput.length(); i  ) {
            if (Character.isLowerCase(userInput.charAt(i))) {
                System.out.print(userInput.toUpperCase().charAt(i));

            } else if (!userInput.contains(".*[a-z].*")) {
                System.out.print(userInput.charAt(i));
            }

        }
    }
}

but this just outputs everything in uppercase. Please help.

CodePudding user response:

Get rid of that regex and just use a simple loop and a flag to indicate that you have process the first LC char

Scanner sc = new Scanner(System.in);
String userInput= sc.next();
boolean done = false;
for (int i = 0; i < userInput.length(); i  ) {
   if (!done && Character.isLowerCase(userInput.charAt(i))) {
      System.out.print(userInput.toUpperCase().charAt(i));
      done = true;
  }
  else  {
      System.out.print(userInput.charAt(i));
  }
      
}

CodePudding user response:

I'm so confused because one I don't know the exact number of indexes because it varies

Yes, that would the reason for the loop

how can I capitalize only the first occurring lowercase

Well, there's a few ways you might do it. String#substring might be starting point, for example...

public class Test {

    public static void main(String[] args) {
        String test = "BYus";

        StringBuilder sb = new StringBuilder(test);

        for (int index = 0; index < sb.length(); index  ) {
            if (Character.isLowerCase(sb.charAt(index))) {
                test = test.substring(0, index)   Character.toUpperCase(test.charAt(index))   test.substring(index   1);
                break;
            }
        }

        System.out.println(test);
    }
}

or, if you want to be a little more efficient, you could use a StringBuilder, for example...

public class Test {

    public static void main(String[] args) {
        String test = "BYus";

        StringBuilder sb = new StringBuilder(test);

        for (int index = 0; index < sb.length(); index  ) {
            if (Character.isLowerCase(sb.charAt(index))) {
                sb.replace(index, index   1, Character.toString(test.charAt(index)).toUpperCase());
                break;
            }
        }
        test = sb.toString();

        System.out.println(test);
    }
}

You could also convert the String to a character array and simply replace the first lower cased character in it and build a new String at the end, for example...

public class Test {

    public static void main(String[] args) {
        String test = "BYus";
        char[] characters = test.toCharArray();

        for (int index = 0; index < characters.length; index  ) {
            if (Character.isLowerCase(characters[index])) {
                characters[index] = Character.toUpperCase(test.charAt(index));
                break;
            }
        }

        test = new String(characters);

        System.out.println(test);
    }
}

So, you know, options

CodePudding user response:

You can iterate over the input as an array of chars:

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string ");
        char[] userInput = sc.next().toCharArray();

        for (int i = 0; i < userInput.length; i  ) {
            if (Character.isLowerCase(userInput[i])) {
                userInput[i] = Character.toUpperCase(userInput[i]);
                break;
            }
        }

        System.out.println(String.valueOf(userInput));
    }
  •  Tags:  
  • java
  • Related