Write a Java program to capitalize the first letter of each word in a sentence.
For this, I have written a java program but it is throwing an error
"result of string.replace(),is ignored";
my code
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the string :");
String a = sc.nextLine();
for(int i = 0; i < a.length(); i ) {
if(a.charAt(i) == ' ') {
char c;
char d;
c = a.charAt((i 1));
d = Character.toUpperCase(c);
a.replace(a.charAt((i 1)),d);
}
}
System.out.println(a);
}
}
I don't know where I did make a mistake.
CodePudding user response:
Java's String.replace function does not do a mutation of the string, it returns a new string with the replacement performed.
See here.
You will need to rethink your design. One approach is to copy the string, and then scan through the original, while progressively replacing the copy.
Something like this:
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the string :");
String a=sc.nextLine();
String b = a;
for(int i=0;i<a.length();i )
{
if(a.charAt(i)==' ')
{ char c;
char d;
c= a.charAt((i 1));
d=Character.toUpperCase(c);
b=b.replace(a.charAt((i 1)),d);
}
}
System.out.println(a);
}
}
You might even be able to just replace a
, like this:
package com.company;
import java.util.Scanner;
public class uppercase_1srchar_of_string {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter the string :");
String a=sc.nextLine();
for(int i=0;i<a.length();i )
{
if(a.charAt(i)==' ')
{ char c;
char d;
c= a.charAt((i 1));
d=Character.toUpperCase(c);
a=a.replace(a.charAt((i 1)),d);
}
}
System.out.println(a);
}
}
CodePudding user response:
Use replaceAll
String a = "Write a Java program to capitalize the first letter of each word in a sentence.";
String b = Pattern
.compile("([^a-zA-Z]*)([a-zA-Z] )")
.matcher(a)
.replaceAll(w ->
w.group(1) w.group(2).substring(0, 1).toUpperCase() w.group(2).substring(1));
System.out.println(b);
with output
Write A Java Program To Capitalize The First Letter Of Each Word In A Sentence.
(Note: the word definition is not clear at all, change it according to your needs see e.g. https://www.jrebel.com/blog/java-regular-expressions-cheat-sheet )
CodePudding user response:
Here is Java's streamy code to do the same without using replace method at all,
import java.util.Arrays;
import java.util.stream.Collectors;
public class HelloWorld{
public static void main(String []args){
String data = "Hello world is my 1st program, wow i am amazed";
String output = Arrays.stream(data.split(" "))
.map(word -> Character.toUpperCase(word.charAt(0)) word.substring(1))
.collect(Collectors.joining(" "));
System.out.println("Output: " output);
}
}
which outputs following,
Output: Hello World Is My 1st Program, Wow I Am Amazed
You can play around above solution online at https://www.tutorialspoint.com/compile_java8_online.php