Pad a String | Write a program in Java to pad a String. | ISC Computer Science | ICSE Computer Application
Algorithm
- Start
- str=""
- ch=''
- len=0
- p=0
- choice=0
- Print "Enter a word"
- Read str
- Print "Enter the character to pad"
- Read ch
- Print "Enter the final length of the string"
- Read len
- p=len-str.length()
- Print "Enter 1 to left pad"
- Print "Enter 2 to center pad"
- Print "Enter 3 to right pad"
- Read choice
- If choice==1. go to step 19 or else go to step 25
- i=0
- If i<p, go to step 21 or else go to step 23
- Print ch
- i++. Go to step 20
- Print str
- Go to step 44
- If choice==2, go to step 26 or else go to step 36
- i=0
- If i<p/2, go to step 28 or else go to step 30
- Print ch
- i++. Go to step 27
- Print str
- i=p/2
- If i<p, go to step 33 or else go to step 35
- Print ch
- i++. Go to step 32
- Go to step 44
- If choice==3, go to step 37 or else go to step 43
- Print str
- i=0
- If i<p, go to step 40 or else go to step 42
- Print ch.
- i++. Go to step 39
- Go to step 44
- Print "Invalid Input"
- Stop
Program
Buffered Reader
import java.io.*;
class Pad
{
String str;
char ch;
int len, p, choice;
void accept() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter a word");
str=br.readLine();
System.out.println("Enter the character to pad");
ch=br.readLine().charAt(0);
System.out.println("Enter the final length of the string");
len=Integer.parseInt(br.readLine());
p=len-str.length();
System.out.println("Enter 1 to left pad");
System.out.println("Enter 2 to center pad");
System.out.println("Enter 3 to right pad");
choice=Integer.parseInt(br.readLine());
}
void leftpad()
{
for(int i=0;i<p;i++)
{
System.out.print(ch);
}
System.out.print(str);
}
void centerpad()
{
for(int i=0;i<p/2;i++)
{
System.out.print(ch);
}
System.out.print(str);
for(int i=p/2;i<p;i++)
{
System.out.print(ch);
}
}
void rightpad()
{
System.out.print(str);
for(int i=0;i<p;i++)
{
System.out.print(ch);
}
}
void main() throws IOException
{
accept();
switch(choice)
{
case 1:
leftpad();
break;
case 2:
centerpad();
break;
case 3:
rightpad();
break;
default:
System.out.println("Incorrect Input");
break;
}
}
}
Scanner
import java.util.*;
class Pad
{
String str;
char ch;
int len, p, choice;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word");
str=sc.next();;
System.out.println("Enter the character to pad");
ch=sc.next().charAt(0);
System.out.println("Enter the final length of the string");
len=sc.nextInt();
p=len-str.length();
System.out.println("Enter 1 to left pad");
System.out.println("Enter 2 to center pad");
System.out.println("Enter 3 to right pad");
choice=sc.nextInt();
}
void leftpad()
{
for(int i=0;i<p;i++)
{
System.out.print(ch);
}
System.out.print(str);
}
void centerpad()
{
for(int i=0;i<p/2;i++)
{
System.out.print(ch);
}
System.out.print(str);
for(int i=p/2;i<p;i++)
{
System.out.print(ch);
}
}
void rightpad()
{
System.out.print(str);
for(int i=0;i<p;i++)
{
System.out.print(ch);
}
}
void main()
{
accept();
switch(choice)
{
case 1:
leftpad();
break;
case 2:
centerpad();
break;
case 3:
rightpad();
break;
default:
System.out.println("Incorrect Input");
break;
}
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
String | str | To store a string |
char | ch | To store the character of padding |
int | len | To store the length of the final string |
int | p | To store the padding length |
int | choice | To store the choice of padding |
int | i | To act as a counter variable |
Method Description
Return type | Method Name | Description |
void | accept() | To accept data from the user |
void | leftpad() | To left align the string |
void | centerpad() | To center align the string |
void | rightpad() | To right align the string |
void | main() | To act as an entry gateway for the compiler. |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Quora: ISC Java Code
Reddit: @ISCJavaCode
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment