Answer to Question #299651 in Java | JSP | JSF for Mesew

Question #299651

import java.util.*;



class App {


public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a String: ");

String line = in.nextLine();

boolean space = true;

boolean first = false;

for (int i = 0; i < line.length(); i++) {

if (line.substring(i, i + 1).equals(" ")) {

System.out.print(line.substring(i, i + 1));

space = !space;

first = false;

} else if (space) {

if (!first) {

System.out.print(line.substring(i, i + 1) + ".");

first = true;

}

} else {

System.out.print(line.substring(i, i + 1));

}

}

System.out.println();

in.close();

}

}

Do the coding without using boolean, array and split


1
Expert's answer
2022-02-19T10:42:21-0500
import java.util.*;



class App {


public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a String: ");

String line = in.nextLine();

int space = 1;

int first = -1;

for (int i = 0; i < line.length(); i++) {

if (line.substring(i, i + 1).equals(" ")) {

System.out.print(line.substring(i, i + 1));

space *= -1;

first = -1;

} else if (space == 1) {

if (first == -1) {

System.out.print(line.substring(i, i + 1) + ".");

first = 1;

}

} else {

System.out.print(line.substring(i, i + 1));

}

}

System.out.println();

in.close();

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS