[Computer Science]
Question in Attachment.
Programming language: Java
[Not a Challenge]
Answers
String program - Java
Here in this question we are asked to write a program to ask a string then display the string in the format given in the Question, before we write the program, let's take some hint and know about that how to create the program. And also known about java and programming.
Java
Java is a high level programming language, it was developed by Sun Microsystems in the early early 1990s, it was designed by different platforms.
Programming
Programming means to create or develop a software which is called a program that contains the instructions to tell a computer what to do.
What we need to do
There are three main ideas in this question. We print number on every char except space so we have to use an if for that.
The number we print must be always under than 10 in this case % 10 is the best way to do it.
Our count "i" will always increase even if the char is space so we have to use sperate counter "j" and increase it just if char isn't space [I increase it when it enter the else], our output start with 1 so j will be 1 when initialise.
The Program
public class program
{
⠀ public static void main(String[] args) {
⠀⠀⠀String str="PROGRAMMING IS INTERESTING";
⠀⠀⠀System.out.println(str);
⠀⠀⠀for (int i=0,j=1;i<str.length();i++) {
⠀⠀⠀if(str.charAt(i)==' ')
⠀⠀⠀System.out.print(' ');
⠀⠀⠀else System.out.print(j++%10);
⠀⠀⠀}
⠀ }
}
The Output
If you run the given program, then the output of the given program will be as follows:
>>> PROGRAMMING IS INTERESTING
⠀⠀ 12345678901 23 45678901234
For better output see the attachment for the output of the given program.
Note: Do not copy-paste the exact cօde and run it, there are some white-space and no-space characters that will need to be removed before use, I've entered them for the sake of showing indentations in the cօde.