Programming Languages:- Repeat Characters

Background

When programming one will have to use various datatypes and consult API for each of the data types.

 

DataTypes

  1. Number/Integer
    • Arithmetic
  2. String
    • Concatenation
    • Length
  3. Character
    • Repeat

 

Character

For instance, if we will like to place an underline against a title, we will get the length of the title, and repeat the underline n times based on the title.

 

Microsoft

Database

SQL Server

Transact SQL


set nocount on;
go

declare @strTitle varchar(60)
declare @ch       char(1);
declare @length   int;

set @ch = '=';
set @strTitle = 'Head of the class';

set @length = len(@strTitle);

print @strTitle;
print replicate(@ch, @length);

Powershell


[string] $title = "";
[char]   $ch = ' ';
[int]    $len = 0;
[string] $filler = "";

function repeatChar([char] $ch,[int] $n)
{

   [String] $str = "";
   
    <#
        $str = [String]::new( $ch, $n);
    #>
    $str = new-object System.String($ch, $n);
    
   return ( $str); 
     
}

$ch = '=';

$title = 'Head of the class';

$len = $title.length;

   
$filler = repeatChar $ch $len;

Write-Host $title;

Write-Host $filler;



.Net

C#


using System;

namespace sample
{
    public class Program
    {
        
        public static void Main(string[] args)
        {
            
            displayHeader();                
        
        }
        
        public static void displayHeader()
        {
                        
            String strTitle;
            char   ch = ' ';
            int    n = 0;
            String strData = null;
            
            ch = '=';
            
            strTitle = "Head of the Class";
            
            // get length
            n = strTitle.Length;
            
            // repeat character n times
            strData = repeatChar(ch, n);
            
            Console.WriteLine(strTitle);
            
            Console.WriteLine(strData);
            
        }
        
        public static String repeatChar(char ch, int n)
        {
            
           String str = new String(ch, n);
           
           return ( str); 
            
        }
    }
}

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s