NetApp Interview Question
Testing / Quality Assurancessubroutine in perl to remove space from front and end of the sentence.
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
#!/usr/bin/perl
# Declare the subroutines
sub trim($);
sub ltrim($);
sub rtrim($);
# Create a test string
my $string = " \t Hello world! ";
# Here is how to output the trimmed text "Hello world!"
print trim($string)."\n";
print ltrim($string)."\n";
print rtrim($string)."\n";
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
my $string = shift;
$string =~ s/\s+$//;
return $string;
}
# include<stdio.h>
# include <conio.h>
void main()
{
int i=0,j=0;
char widspc[80],wospc[80];
printf("\n Enter A string with blank spaces: ");
gets(widspc);
while (widspc[i]!='\0')
{
if (widspc[i] != ' ')
{
wospc[j]=widspc[i];
j=j+1;
}
i=i+1;
}
wospc[j+1]='\0';
printf("\n");
puts(wospc);
}
#!/usr/bin/perl
#My First Solution
$myString = " I need To Remove Spaces";
@myArray = split(' ',$myString);
$myString = '';
foreach $a (@myArray){
$myString = $myString.$a;
print "$a\n"
}
print "$myString\n";
#solution with regex
$myString =" I need To remove Spaces";
$myString =~ s/\s//g;
print "$myString\n";
## Given string, needs to squeze
- Anonymous December 09, 2010my $string = "squeezing spaces fro m t h e string";
## regex that squeeze the spaces
$string =~ s/\s//g;
print "$string \n";