Question
program to replace white spaces with a specific character in a given sentence.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class replaceWhiteSpacesToSpecificCharacterInSentence
{
public static void main(String[] args)
{
String sen="",wd="";
char ch=' ',ch1=' ';
int i=0,len=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
sen = sc.nextLine();
System.out.println("Enter the character that replaces white spaces");
ch1 = sc.next().charAt(0);
len=sen.length();
for(i=0;i< len;i++)
{
ch=sen.charAt(i);
if(ch==' '||i==(len-1))
{
wd=wd+ch1;
System.out.print(wd);
wd="";
}
else
{
wd=wd+ch;
}
}
}
}