कंप्यूटर प्रोग्रामिंग/स्ट्रिंग्स/पर्ल

विकिविश्वविद्यालय से

strings.pl[सम्पादन | स्रोत सम्पादित करें]

#!/usr/bin/perl

# This program splits a given comma-separated name into first and last name
# components and then displays the name.

sub get_name
{
    my $name;
    my $index;
    
    do
    {
        print("Enter name (last, first):\n");
        $name = <>;
        chomp($name);
        $index = index($name, ",")
    } while $index == -1;
    
    return $name;
}

sub get_last
{
    my ($name) = @_;
    my $last;
    my $index;

    $index = index($name, ",");
    if ($index == -1)
    {
        $last = "";
    }
    else
    {
        $last = substr $name, 0, $index;
    }
    
    return $last;
}

sub get_first
{
    my ($name) = @_;
    my $first;
    my $index;

    $index = index($name, ",");
    if($index == -1)
    {
        $first = "";
    }
    else
    {
        $first = substr $name, $index + 1;
        $first = ltrim($first);
    }

    return $first;
}

sub display_name
{
    my ($first, $last) = @_;
    
    print("Hello " . $first . " " . $last . "!\n");
}

sub ltrim
{
    my ($text) = @_;
    
    if ((substr $text, 0, 1) == " ")
    {
        $text = substr $text, 1
    }
    
    return $text;
}

sub main
{
    my $name;
    my $last;
    my $first;

    $name = get_name();
    $last = get_last($name);
    $first = get_first($name);
    display_name($first, $last);
}

main()

कोशिश करो[सम्पादन | स्रोत सम्पादित करें]

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें[सम्पादन | स्रोत सम्पादित करें]