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"
1.) Many languages (e.g., C and Java) distinguish the character ’c’ from the string “c” with...
1.) Many languages (e.g., C and Java) distinguish the character ’c’ from the string “c” with separate sets of quotation marks. Others (e.g., Python) use “c” for both single characters and strings of length one. Provide (and justify) one advantage and one disadvantage of Python’s approach. 2.The designers of Java distinguish the primitive types (scalars) from the reference types (all other types of values) in the language. Discuss the costs and benefits to the programmer of this decision 3.In Ruby,...
A certain programming language allows only two-character variable names. The first character must be a letter...
A certain programming language allows only two-character variable names. The first character must be a letter (upper or lower case) and the second character can be a letter (upper or lower case) or a digit. How many possible variable names are there?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT