-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathstringPadding.java
More file actions
40 lines (32 loc) · 958 Bytes
/
stringPadding.java
File metadata and controls
40 lines (32 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//padding a String
//Input: str = “PaddingString”, ch =’-‘, L = 20
//Output:
//Left Padding: -------PaddingString
//Right Padding: PaddingString-------
import java.lang.*;
import java.io.*;
public class padString {
// Function to perform left padding
public static String leftPadding(String input, char ch, int L)
{
String result = String.format("%" + L + "s", input).replace(' ', ch);
// Returning the result
return result;
}
// Function to perform right padding
public static String rightPadding(String input, char ch, int L)
{
String result = String.format("%" + (-L) + "s", input).replace(' ', ch);
// Returning the result
return result;
}
public static void main(String[] args)
{
String str = "StringPadding";
char ch = '-';
int L = 20;
System.out.println(leftPadding(str, ch, L));
System.out.println(centerPadding(str, ch, L));
System.out.println(rightPadding(str, ch, L));
}
}