Trying a String exercise in JAva

In summary, the cap method takes in a string and splits it into an array of words. It then iterates through the array, capitalizing the first letter of each word and adding it to a new string. It then appends the rest of the word to the capitalized first letter and adds a space before moving onto the next word. This method effectively capitalizes the first letter of every word in a sentence.
  • #1
camel-man
76
0
I am trying to capitalize the first letter of every word in a sentence.
Example. "hey what's up" would be "Hey What's Up".

here is my code but nothing is changing the sentence. I thought everything was right.

Code:
	public static String cap(String aString) {

		String[] sentence = aString.split(" ");
		String b = "";
		for (int i = 0; i < sentence.length; i++) {
			Character.toUpperCase(sentence[i].charAt(0));
			b += sentence[i] + " ";
		}

		return b;

	}
 
Technology news on Phys.org
  • #2
The toUpperCase method doesn't change the parameter you pass to it. It returns the converted character.

So you want to do something like
Code:
b += Character.toUpperCase(sentence[i].charAt(0));
and then append the rest of the "word" to b.
 
  • #3
Thank you for the tip. So does this suffice? How bad is it and are there easier ways to accomplish this?

Code:
public static String cap(String aString) {

		String[] sentence = aString.split(" ");
		String b = "";
		for (int i = 0; i < sentence.length; i++) {
			char temp = Character.toUpperCase(sentence[i].charAt(0));
			b += temp;
			for(int j = 1; j<sentence[i].length(); j++)
				b += sentence[i].charAt(j);
			
			b += " ";
		}

		return b;

	}
 

FAQ: Trying a String exercise in JAva

1. What is a String exercise in Java?

A String exercise in Java refers to practicing and manipulating strings, which are a sequence of characters, in the Java programming language. It involves using various methods and functions to perform operations on strings, such as concatenation, splitting, and replacing.

2. Why is it important to practice String exercises in Java?

String exercises in Java are important because strings are a fundamental data type in programming and are used extensively in applications. By practicing and becoming proficient in manipulating strings, programmers can improve their problem-solving skills and develop more efficient and effective code.

3. What are some common String exercises in Java?

Some common String exercises in Java include reversing a string, checking if a string is a palindrome, finding the length of a string, and removing duplicate characters from a string. These exercises help develop skills in string manipulation and familiarize programmers with different methods and functions available for strings in Java.

4. How can I improve my skills in String exercises in Java?

One way to improve skills in String exercises in Java is to practice regularly and challenge yourself with different problems. Additionally, reading and understanding the documentation for string-related methods and functions in Java can also help in improving proficiency.

5. Are there any resources available for learning String exercises in Java?

Yes, there are many resources available for learning String exercises in Java, such as online tutorials, coding platforms, practice problems, and books. Some popular resources include Codecademy, HackerRank, and Java Strings: A Beginner's Guide by Herbert Schildt.

Similar threads

Back
Top