Question

In: Computer Science

(In java) Return a copy of the string with only its first character capitalized. You may...

(In java) Return a copy of the string with only its first character capitalized. You may find the Character.toUpperCase(char c) method helpful

NOTE: Each beginning letter of each word should be capitalized. For example, if the user were to input: "United States of America " --> output should be "United States of America" NOT "United states of america"

--------------------------------------

(This code is given)

public class Class1 {

public static String capitalize(String str) {
    //add code here
}
}

Solutions

Expert Solution

/*If you any query do comment in the comment section else like the solution*/

import java.util.Scanner;
public class CapitalizeFirstCharacter {
        public static void main(String[]args) {
                Scanner sc = new Scanner(System.in);
                String input = sc.nextLine();
                String res = "";
                String splitInput[] = input.split(" ");
                for(String s: splitInput) {
                        String firstCharacter=s.substring(0,1);  
                String restCharacter=s.substring(1);  
                res = res + firstCharacter.toUpperCase() + restCharacter + " "; 
                }
                System.out.println(res);
        }
}


Related Solutions

Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
(In JAVA)Write code to print the location of any alphabetic character in the 2-character string passCode.
Java Language Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline. Ex: If passCode is "9a", output is: Alphabetic at 1 import java.util.Scanner;   public class FindAlpha {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String passCode;              passCode = scnr.next();     ...
What is the best running time taken to print the first character of a string of...
What is the best running time taken to print the first character of a string of length n? O(1) O(log n) O(n) O(n log n) O(n 2) O(n k) where k is a constant > 2 O(2 n) What is the best running time taken to print the letter 'A'. O(1) O(log n) O(n) O(n log n) O(n 2) O(n k) where k is a constant > 2 O(2 n) What is the best running time taken to determine the...
JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character represents the first letter...
JAVA MASTERMIND The computer will randomly select a four-character mastercode. Each character represents the first letter of a color from the valid color set. Our valid color choices will be: (R)ed, (G)reen, (B)lue and (Y)ellow. Any four-character combination from the valid color set could become the mastercode. For example, a valid mastercode might be: RGBB or YYYR. The game begins with the computer randomly selecting a mastercode. The user is then given up to 6 tries to guess the mastercode....
In Java, write a program that finds the first character to occur 3 times in a...
In Java, write a program that finds the first character to occur 3 times in a given string? EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /**...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /** * This class represents a DVD player to be rented. * * @author Franklin University * @version 2.0 */ public class DVDPlayer extends AbstractItem { /** * Constructor for objects of class DVDPlayer. */ public DVDPlayer() { // No code needed } /** * Creates a DVDPlayer from a string in the format * id:desc:weeklyRate:rented * @param string The string * @return the new...
Writing method in Java Given a string, return the sum of the numbers appearing in the...
Writing method in Java Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.) sumNumbers("abc123xyz") → 123 sumNumbers("aa11b33") → 44 sumNumbers("7 11") → 18
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
I need to write java program that do replace first, last, and remove nth character that...
I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods. No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive. if user's input was "be be be" and replace first 'b' replace first should do "e be be" replace last should do "be be e" remove nth character such as if user want to remove 2nd b it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT