Question

Write a Java program that that capitalizes all first letters of the words in the given...

Write a Java program that that capitalizes all first letters of the words in the given String. All other symbols should be intact. If a word does not start with a letter, it should remain intact as well. Assume that the parameter String can only contain spaces and alphanumeric characters.

Example:

Input: “100 234 jus 23 jskl”

Output: “100 234 Jus23 jskl”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Coding

    import java.util.Scanner;
    class first
    {
  
   public static String ConvertoCapitlize(String givenString)//here is the function
{
    String[] arr = givenString.split(" ");
    StringBuffer sb = new StringBuffer();
   int j=0,dig=0;
   int i=0;
   try {
      for (i = 0; i < arr.length; i++) {
   boolean flag=false;
   char ch=arr[i].charAt(0);
   if(Character.isDigit(ch)==true)
   {
       flag=true;
       j=0;
       dig=0;
       for (char c : arr[i].toCharArray())
       {

           if(Character.isDigit(c))
           {
           dig++;
           }
           else
           {
           break;
           }
           j++;
       }
  
   sb.append(arr[i].substring(0,j)).append(Character.toUpperCase(arr[i].charAt(j)))
            .append(arr[i].substring(j+1)).append(" ");
   }
   else if(Character.isDigit(ch)==false && flag==false)
   {
   sb.append(Character.toUpperCase(arr[i].charAt(0)))
            .append(arr[i].substring(1)).append(" ");
   }
   else{
   sb.append(arr[i].charAt(0))
            .append(arr[i].substring(1)).append(" ");
   }
    }
   }
        catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Warning: ArrayIndexOutOfBoundsException");
       }
     catch(Exception e){

     }      
    return sb.toString().trim();
}
       public static void main(String args[])
       {
          String s,s1;//here is the declare variable
          Scanner in = new Scanner(System.in);//get user input from user
          System.out.println("Enter a string");
          s = in.nextLine();
          System.out.println("You entered string " + ConvertoCapitlize(s));//convert it to capitilize function
       }
    }

output:

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........

Add a comment
Know the answer?
Add Answer to:
Write a Java program that that capitalizes all first letters of the words in the given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first...

    PLEASE WRITE SIMPLE JAVA PROGRAM AND ALSO EXPLAIN THE LOGIC. Given a string, print the first word which has its reverse word further ahead in the string. Input Format: A string of space separated words, ending with a 's'. Conditions ; Each string ends with a $ character. All characters in the string are either spaces. $ or lower case English alphabets, Output Format : In a single line pent either . The first word from the start of the...

  • Write a C program that prompts the user for an input string with all lowercase letters....

    Write a C program that prompts the user for an input string with all lowercase letters. Your program should then sort the characters in the string and print the sorted string. Assume that the string contains no spaces and has at most 30 lowercase characters. Your program should loop continually until the user enters “q” to quit.

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • Write a program that reads in a sentence of up to 100 characters and outputs the...

    Write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all strings of two or more blanks should be compressed to a single blank. The sentence should start with an uppercase letter but should contain no other uppercase letters. Do not worry about proper names; if their first letters are changed to lowercases, that is acceptable. Treat...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “user123@domain.ext”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • 3. (20 points) Write a program that writes to output 1000 random 'words', separated by spaces...

    3. (20 points) Write a program that writes to output 1000 random 'words', separated by spaces (word simply means a sequence of alphabetical letters, not necessarily an actual word that can be found in the dictionary). The 'words' you write should be randomly generated sequences of lowercase letters, with random lengths between 3 and 9 characters. Your program must save the word as a string and then print it out using the percent-s format in printf.

  • Make a C program to count the number of occurrences of words from the input. For...

    Make a C program to count the number of occurrences of words from the input. For example, with input "one two one three one two" your program should output: one 3 two 2 three 1 It should work for up to 100 different words. If there are more than 100 unique words in the input, the program should still work, and count the number of appearances of the first 100 unique words. Each word should have the same maximum amount...

  • Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array...

    Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array listing words and their counts in sorted order Function Description: Have you ever seen those fancy word-clouds based on the frequency of word occurrences in some text? Well the first step of making a graphic like that is to figure out how often the words in some text occur, which is exactly what this function will do. You will count the number of occurrences...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT