Answer:
Following is the definition of method padString in Java:
public static String padString(String str, int length)
{
int i = 0;
String temp = str;
if(length>str.length())
{
while (i<(length - str.length()))
{
temp = " " + temp;
i++;
}
}
return temp;
}
Step-by-step explanation:
Sample program to demonstrate above method:
public class Main
{
public static void main(String[] args) {
System.out.println(padString("Hello", 8));
System.out.println(padString("congratulations", 10));
}
public static String padString(String str, int length)
{
int i = 0;
String temp = str;
if(length>str.length())
{
while (i<(length - str.length()))
{
temp = " " + temp;
i++;
}
}
return temp;
}
}
output:
Hello
congratulations