Skip to content

Adding a String Padding Program #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions 07 - Strings - Working With Text/src/stringPadding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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));
}
}