Pad a String | Write a program in Java to pad a String. | ISC Computer Science | ICSE Computer Application


Image is Unavailable


Algorithm

  1.   Start
  2. str=""
  3. ch=''
  4. len=0
  5. p=0
  6. choice=0
  7. Print "Enter a word"
  8. Read str
  9. Print "Enter the character to pad"
  10. Read ch
  11. Print "Enter the final length of the string"
  12. Read len
  13. p=len-str.length()
  14. Print "Enter 1 to left pad"
  15. Print "Enter 2 to center pad"
  16. Print "Enter 3 to right pad"
  17. Read choice 
  18. If choice==1. go to step 19 or else go to step 25
  19. i=0
  20. If i<p, go to step 21 or else go to step 23
  21. Print ch
  22. i++. Go to step 20
  23. Print str
  24. Go to step 44
  25. If choice==2, go to step 26 or else go to step 36
  26. i=0
  27. If i<p/2, go to step 28 or else go to step 30
  28. Print ch
  29. i++. Go to step 27
  30. Print str
  31. i=p/2
  32. If i<p, go to step 33 or else go to step 35
  33. Print ch
  34. i++. Go to step 32
  35. Go to step 44
  36. If choice==3, go to step 37 or else go to step 43
  37. Print str
  38. i=0
  39. If i<p, go to step 40 or else go to step 42
  40. Print ch.
  41. i++. Go to step 39
  42. Go to step 44
  43. Print "Invalid Input"
  44. 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 NameDescription
 Stringstr To store a string 
 charch To store the character of padding 
 intlen To store the length of the final string 
int To store the padding length 
 intchoice To store the choice of padding 
 intTo act as a counter variable 

Method Description

 Return typeMethod NameDescription
 voidaccept() To accept data from the user 
 voidleftpad() To left align the string 
 voidcenterpad() To center align the string 
 void rightpad()To right align the string 
 voidmain() To act as an entry gateway for the compiler. 

Output

Image is Unvailable
Image is UnavailableImage is UnavailableImage is Unavailable




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
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful


Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences