Amazon Interview Question
Testing / Quality Assurances#include <iostream>
#include <cmath>
using namespace std;
void ReverseStringAndNumber(string str, int num)
{
// String first
int n = str.length();
char temp;
for(int i = 0; i < n / 2; i++)
{
temp = str[n-i-1];
str[n-i-1] = str[i];
str[i] = temp;
}
cout << str.c_str() << endl;
// Number
int rem = 0;
while(num>0)
{
rem = (rem * 10) + (num % 10);
num = num / 10;
}
cout << rem << endl;
}
void main()
{
ReverseStringAndNumber("canku", 21302341);
}
Perl code to reverse the string
use POSIX; # for using floor function
chomp($str= <STDIN>);
@string = split ('',$str); # splitter will come first
$len=@string; # length of array
print "Array Length = $len ";
$n=floor((@string)/2);
for($i = 0 ; $i < $n ; $i++) # necessary to use $ with all variable
{
$temp=$string[$i];
$string[$i]=$string[$len-$i-1];
$string[$len-$i-1]=$temp;
}
$string = join ('',@string);
print "reversed string is :- ".$string ."\n";
www udzial com
Udzial Means Share
//To reverse a string without using inbuilt function
- Ayesha September 26, 2012for(i=0;string[i]!='\0';i++);
n=i;
for(i=n-1;i>=0;i--)
reverse[i]=string[i];