Java: Create a new string taking first three characters from a given string
First 3 Chars or #
Write a Java program to create a string taking the first three characters from a given string. If the string length is less than 3 use "#" as substitute characters.
Test Data: str1 = "Python"
str2 = " "
Sample Solution:
Java Code:
Sample Output:
###
Pictorial Presentation:
Flowchart:
For more Practice: Solve these Related Problems:
- Modify the program to replace missing characters with "*".
- Write a program to take the last 3 characters instead of the first.
- Modify the program to repeat the extracted characters twice.
- Write a program to handle empty strings without errors.
Go to:
PREV : Remove First Char and Concatenate.
NEXT :
First and Last Char Combo.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.