Operators/String Duplication

From FizzFuzz

Jump to: navigation, search
Go back to Operators.

String duplication is a non-commutative, non-associative operation performed on a character-based data types (either a char or a string) and an int, which produces a string. It is binary operator indicated by an infix * (an asterisk).

Contents

Notation

α * δ

Where α is a char or string and δ is a int.

Assignment

Α *= δ
Α = Α * δ

Where:

  • Α is a char or string, the value being altered.
  • δ is an int.

The first example is shorthand for the second.

Operation

This operator takes the string or char α and repeats it the number of times indicated by the int δ. It acts as iterated concatenation, and is the functional equivalent of the following:

α * δ = α * |δ| = α1 + α2 + ... + α|δ|-1 + α|δ|

Where + is the concatenation operator and αn indicates the nth instance of α.

Code Implementation

Below is an implementation of this operation using FizzFuzz.

string generic<string T, char T> StringDuplicate(T alpha, int delta)
{
    delta.abs();
 
    string result = null;
    for(int i = 1; i <= delta; i ++)
    {
        result += alpha;
    }
 
    return result;
}

Example

char alpha = 'a';
string beta = "beta";
 
output << alpha * 10; // Outputs "aaaaaaaaaa".
output << beta * 3; // Outputs "betabetabeta".

See Also