Converting first letter of each word to uppercase [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know if there is another method other than WordUtils.capitalize() that converts the first letter of each word to upper case?

You could use a method you create:
String CapsFirst(String str) {
String[] words = str.split(" ");
StringBuilder ret = new StringBuilder();
for(int i = 0; i < words.length; i++) {
ret.append(Character.toUpperCase(words[i].charAt(0)));
ret.append(words[i].substring(1));
if(i < words.length - 1) {
ret.append(' ');
}
}
return ret.toString();
}

public static String caseFirst(String givenString) {
String[] a= givenString.split(" ");
StringBuffer s= new StringBuffer();
for (int i = 0; i < a.length; i++) {
s.append(Character.toUpperCase(a[i].charAt(0))).append(a[i].substring(1)).append(" ");
}
return s.toString().trim();
}

Related

Change the case of every alternate char [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to write a code in Java which does make changes to case of a alphabet character in an alternating fashion(either make it lowercase or uppercase)
For Example:
changeCapitalization("hey 123 ABC idk"); // hEy 123 AbC iDk
changeCapitalization("abcdef ghijk 12 abc"); // aBcDeF gHiJk 1 AbC
You can use StringBuilder to build new string and boolean marker to change between lower/upper letter case.
public static String changeCapitalization(String str) {
StringBuilder buf = new StringBuilder(str.length());
boolean upperCase = false;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch)) {
buf.append(upperCase ? Character.toUpperCase(ch) : Character.toLowerCase(ch));
upperCase = !upperCase;
} else
buf.append(ch);
}
return buf.toString();
}
public static void main(String[] args) {
String str ="hello THERE";
String new_str="";
for(int i=0;i<str.length();i++){
String c =Character.toString(str.charAt(i));
if(Character.isUpperCase(str.charAt(i))){
c=c.toLowerCase();
new_str+=c;
}
else if(Character.isLowerCase(str.charAt(i))){
c=c.toUpperCase();
new_str+=c;
}
else if(c.equals(" ")){
new_str+=" ";
}
}
System.out.println(str);
System.out.println(new_str);
}

How to add spaces in between numbers contained inside a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have string as "DOC_87654321 -ABC76543". What I want is add spaces in between numbers. The result should be "DOC_8 7 6 5 4 3 2 1-ABC7 6 5 4 3". How can it possible?
Try this
String str = "DOC_87654321 -ABC76543";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
sb.append(c + " ");
} else {
sb.append(c);
}
}
Log.e("DATA",sb.toString());
you can use this:
String str = "DOC_87654321 -ABC76543- 959565412";
String finalStr = "";
String[] split = str.split("-");
for (int i = 0; i < split.length; i++) {
finalStr +=
split[i]
.replaceAll("([0-9])", "$1 ")
.trim()
+ (i != split.length - 1 ? '-' : "");
}
System.out.println("finalStr = " + finalStr);
In case if you don't wanna use Character.isDigit() you can compare their ascii decimal values and use it like this
public static void main(String[] args) {
String str = "DOC_87654321 -ABC76543";
StringBuilder string = new StringBuilder();
for(int i=0;i<str.length();i++) {
if(str.charAt(i) >= 48 && str.charAt(i) <=57) {
string.append(str.charAt(i)+" ");
}else {
string.append(str.charAt(i));
}
}
System.out.println(string.toString());
}

How to rewrite code from array to arraylist without using .asList? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Teacher gave this code and said to rewrite it so that there is no array and just rewritten to arraylist. I am not allowed to use .asList. Any idea how to do this?
public APCSStudents(Student[] sarray) {
for (int i = 0; i < sarray.length; i++)
studentarray[i] = sarray[i];
}
public Student bestStudent() {
Student s = studentarray[0];
for (int i = 0; i < studentarray.length; i++){
if (studentarray[i].getPoints() > s.getPoints()) {
s = studentarray[i];
}
}
return s;
}
Just hints, equivalences:
Student[] studentsArray = new Student(100);
List<Student> students = new ArrayList<>();
studentsArray.length;
students.size();
// See also students.isEmpty()
s = studentsArray[i];
s = students.get(i);
studentsArray[i] = s;
students.set(i, s);
students.add(s); // Adding to a growing array
The Javadoc of List may prove interesting.

What would the java code be for sorting the characters in each word of a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't want a string with the words sorted alphabetically. I need the letters of each word arranged alphabetically, but the order of words to remain the same.
Eg: Input string: welcome to java, Output string: ceelmow ot aajv
Try this:
String str = "welcome to java";
String strs[] = str.split(" ");
char[] ch;
StringBuilder strBuilder = new StringBuilder(str.length());
for (int i=0; i<strs.length; i++) {
ch = strs[i].toCharArray();
Arrays.sort(ch);
strBuilder.append(ch);
if (i != strs.length - 1) {
strBuilder.append(" ");
}
}
System.out.println(strBuilder.toString());
This could be solved using 5 steps
1) Split the String to words using foo.split(" ")
2) Get all the characters in the String using char[] bar=foo.toCharArray()
3) Sort the array using Arrays.sort(bar)
4) Turn the characters in a String using new String(bar)
5) Put all the characters back to a sentence
Don't forget to mind that capitals will come before not-capitals
Let me know if it works (or not)
Happy coding :) -Charlie
import java.util.Arrays;
public class StackOverflowExample {
public static void main(String[] args) {
String s = "welcome to java";
String[] words = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.length; i++) {
if (i != 0) {
sb.append(" ");
}
char[] wordCharArray = words[i].toCharArray();
Arrays.sort(wordCharArray);
sb.append(wordCharArray);
}
System.out.println(sb.toString());
}
}

Printing out vowels using For Loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
String ch = "aeiouycbdfgh";
int len = r.nextInt(10) + 10;
StringBuffer sb = new StringBuffer();
for (int i=0; i<len; i++){
sb.append(ch.charAt(r.nextInt(ch.length())));
}
System.out.println("String:" + sb);
System.out.println("Vowels:");
outputVowels(sb.toString());
}
public static void outputVowels(String s){
How do i make a for loop that will output the vowels of the ch string each in a different line?
edit: the program is meant to output
a
e
i
o
u
First prepare global vowel set - for fast access:
Set<Character> vowelSet = new HashSet<>();
vowelSet.addAll(Arrays.asList('a', 'e', 'i', 'o', 'u'));
Second, you can write scanning loop like this:
String str = "ahjuekjdf";
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if(vowelSet.contains(c)) {
System.out.println(c);
}
}
You can directly use Regex
public class Test {
public static void main(String[] args) {
String s = "Ankur";
s = s.replaceAll("[^aeiouAEIOU]", "");
System.out.println(s);
}
}
Output
Au
change your loop to
for (int i=0; i<len; i++) {
char c = sh.charAt(i);
if ((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')) {
sb.append(ch.charAt(r.nextInt(ch.length())));
}
}
for(char v : vowel){
if(ch == v){
cout <<ch<<" is vowel";
break;
}
else{
cout <<ch<<" is consonant";
break;
}
}

Categories

Resources