Anzeige
(JAVA) Returns a new String consisting of all of the letters of s- but.docx
(JAVA) Returns a new String consisting of all of the letters of s- but.docx
Nächste SlideShare
program for remove unnecessary space from a given input string.imp.pdfprogram for remove unnecessary space from a given input string.imp.pdf
Wird geladen in ... 3
1 von 2
Anzeige

Más contenido relacionado

Similar a (JAVA) Returns a new String consisting of all of the letters of s- but.docx(20)

Más de pjoseph6(20)

Anzeige

(JAVA) Returns a new String consisting of all of the letters of s- but.docx

  1. (JAVA) Returns a new String consisting of all of the letters of s, but where tab characters ('t') are replaced with n spaces public static String tabToSpace(String s, int n) { } public static void main(String args[]) { System.out.print("Enter a word: "); input = scan.nextLine(); System.out.println(tabToSpace(, )) Solution TabReplace.java import java.util.Scanner; public class TabReplace { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); System.out.println("Enter a word :"); String input = scan.nextLine(); System.out.println("Enter Number of Spaces :"); int n = scan.nextInt(); System.out.println(tabToSpace(input, n)); } public static String tabToSpace(String s, int n){
  2. String spaces = ""; String newString = ""; for(int i=0; i<n; i++){ spaces = spaces + " "; } for(int i=0; i<s.length(); i++){ if(s.charAt(i) == 't') newString = newString + spaces; else newString = newString + s.charAt(i); } return newString; } } Output: Enter a word : Hi  Good Morning!  how are     you Enter Number of Spaces : 1 Hi Good Morning! how are you
Anzeige