Register
Sign In
Skip over navigation
Home
Add New Question
Jobs
Products
Reverse a string
Question posted
in
Computer Software
on
03 2010
Rate question difficulty level
0
0 Votes
one of the most common questions given to a developer is to take a string of characters and reverse it. Since Strings are immutable in almost all languages, it is supposed to be tricky.
Add an answer
Share
|
3 Answers
Shirly Leibovich
Any answer that shows the candidate thinking and figuring out the problem.
Examples : credits to
Jim Burns
//------------------------------------------------------
// #1, While easy to understand suffers from a lot of memory reallocation.
// Each time the next letter is added to s2, it's added to the beginning of the
// string causing a reallocation of the entire string.
//
function
ReverseString( s : string ) : string;
var
i : integer;
s2 : string;
begin
s2 :=
''
;
for
i := 1
to
Length( s )
do
s2 := s[ i ] + s2;
Result := s2;
end
;
//------------------------------------------------------
// #2, Taking advantage of the fact that we can work at both ends of the string
// at once AND the fact that IF there is a middle character, ie. an odd number
// of characters in the string, it doesn't change position at all and we can
// eliminate all the memory allocations, work completely within the source
// string swapping from end to end working toward the middle and only having to
// make 1/2 of a loop through the string.
//
procedure
ReverseStr(
var
Src : string);
var
i, j : integer;
C1 : char;
begin
j := Length(Src);
for
i := 1
to
(Length(Src) div 2)
do
begin
C1 := Src[i];
Src[i] := Src[j];
Src[j] := C1;
Dec(j);
end
;
end
;
//------------------------------------------------------
// #3, One disadvantage of #2 can be seen when trying to fill one control with
// the contents of another. For example, two TEdits. Since TEdit.Text can't
// be sent as a var parameter you'll need to first make use of a temporary
// string and then set the second TEdit:
//
// var
// tStr : string;
// begin
// tStr := Edit1.Text;
// ReverseStr(tStr);
// Edit2.Text := tStr;
//
// However, using #3 this code turns into,
//
// Edit2.Text := ReverseStr(Edit1.Text);
//
// In addition, we lost 1 local var and the loop body was reduced since we
// could use Result directly swapping as we go!
//
function
ReverseStr(
const
Src : string) : string;
var
i, j : integer;
begin
j := Length(Src);
SetLength(Result, j);
for
i := 1
to
(Length(Src) div 2)
do
begin
Result[i] := Src[j];
Result[j] := Src[i];
Dec(j);
end
;
end
;
03/24/2010
Shirly Leibovich
in PHP, you can simply use the strrev() function:
$string = "test string";
$string = strrev($string);
http://us2.php.net/manual/en/function.strrev.php
05/19/2010
tn pas chere
Nike TN Pas Cher
TN Pas Cher
Nike Requin
Nike TN
Nike Air Jordans
Cheap Jordan Shoes
AIR Jordans
Nike Jordan Shoes
Nike Air Jordans
Nike Tn Pas Cher
Sac a main
Sac a main femme
sac a dos
valise
bagage à main et maroquinerie en ligne - Gsell
TN Nike
TN Requin
TN Pas Cher
TN Air Max
TN Pas Cher
Nike tn 2012
tn foot locker
TN REQUIN
Nike TN
Nike Requin
Chaussure Requin
Air Max Requin TN Pas Cher
Puma Pas Cher
04/29/2012
Add an answer*
Your name
Email
Enter the code shown above:
please wait..
Company:
Amazon
Location:
United States
Tags:
string
,
development
,
dev
Occupations:
Software\Web Development
add a question
Now hiring!
Software Development Engineer - Cyber Analytics
Amazon
Seattle, WA
technology leadership skills Posted Date 5152012 Company Amazon Corporate LLC Algorithms, Apache, BASIC, Computer, ... Engineers, IT, Java, Modeling, Object Oriented Design, Performance, Protocols, Security, Software, Systems,...
---------------------------
Head of Software Development
Amazon Corporate
Seattle, WA
software engineering manager you will own the design, development, testing, deployment, and operations of an AWS ... ** For more information on Amazon Web Services, please visit http://aws.amazon.com ** A proven track record of building...
---------------------------
Software Development Engineer - AWS Cloudfront
Amazon
Seattle, WA
Amazon Web Services is looking for a software engineer to join our CloudFront content distribution team. Our projects ... For more information on Amazon Web Services, please visit http://aws.amazon.com...
---------------------------
Software Development Manager - Information Security Monitoring and Cyber Analytics
Amazon
Seattle, WA
software engineering practices best practices for the full software development life cycle including coding standards ... Company Amazon Corporate LLC Algorithms, Apache, BASIC, Development, Development Manager, Engineering, Engineers,...
---------------------------
Senior Software Development Manager - AWS Database Services
Amazon Corporate
Seattle, WA
Amazon Web Services (AWS) provides companies of all sizes with an infrastructure web services platform in the cloud. ... See details at http://aws.amazon.com. Database Services team within AWS is responsible for setting the database...
---------------------------
Senior Software Development Engineer, Amazon Mom
Amazon
Seattle, WA
faster and more efficiently? We are building a brand new software development team from the ground up. It's a perfect ... - Experience designing unit, integration, functional and regression tests - Development experience on iPhone or...
---------------------------
Head of Software Development - SimpleDB
Amazon Corporate
Seattle, WA
Amazon Web Services (AWS) provides companies of all sizes with an infrastructure web services platform in the cloud. ... See details at http://aws.amazon.com. Database Services team within AWS is responsible for setting the database...
---------------------------
Mobile Software Developer
Amazon
Seattle, WA
experience - 2+ years professional experience in software development - Computer Science fundamentals in ... - Knowledge of professional software engineering practices & best practices for the full software development life cycle...
Jobs
by
Simply
Hired
View all jobs