The source code to perform left padding without using PadLeft() method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to perform left padding
//without using PadLeft() method.
using System;
class Demo
{
static string StrPadLeft(string str, char ch, int num)
{
string result = "";
for (int i = 0; i < num; i++)
{
result += ch;
}
result += str;
return result;
}
static void Main(string[] args)
{
string Str = "";
string paddedStr= "";
Console.Write("Enter a string: ");
Str = Console.ReadLine();
paddedStr=StrPadLeft(Str, '$', 5);
Console.WriteLine("Padded String: " + paddedStr);
}
}
Output:
Enter a string: Includehelp
Padded String: $$$$$Includehelp
Press any key to continue . . .
Explanation:
Here, we created two static methods StrPadLeft() and Main(). The StrPadLeft() method is used to pad the string with specified character by a given number of times.
In the Main() method, we read the value of the string and passed to the StrPadLeft() method that returned the left padded string and then finally prints the result on the console screen.
Program:
The source code to perform left padding without using PadLeft() method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
Here, we created two static methods StrPadLeft() and Main(). The StrPadLeft() method is used to pad the string with specified character by a given number of times.
In the Main() method, we read the value of the string and passed to the StrPadLeft() method that returned the left padded string and then finally prints the result on the console screen.