C# program to perform the right padding without using the PadRight() method

Here, we are going to learn how to perform the right padding without using the PadRight() method in C#?
Submitted by Nidhi, on October 12, 2020

Here, we will read a string and pad specified character into the right side of the string.

Program:

The source code to perform the right padding without using the PadRight() method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to perform right padding 
//without using PadRight() method. 

using System;

class Demo
{
    static string StrPadRight(string str, char ch, int num)
    {
        string result = "";

        result += str;
        for (int i = 0; i < num; i++)
        {
            result += ch;
        }

        return result;
    }
    static void Main(string[] args)
    {
        string Str      =   "";
        string paddedStr=   "";

        Console.Write("Enter a string: ");
        Str = Console.ReadLine();

        paddedStr=StrPadRight(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 StrPadRight() and Main(). The StrPadRight() 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 StrPadRight() method that returned the right padded string and then finally prints the result on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.