Step-by-step solution for First Reverse
View the challenge

This challenge requires you to reverse an input string. A string in programming is sequence of characters and a character is simply a symbol that you can enter for the computer to read, such as a,b,z,$,4,? etc. An example of a string may be: "Hello" or "abc123$??." Strings always need to be written between double or single quotes (" or '). There exist several built-in functions in almost all languages that reverse a string for you very easily, so we'll cover how to first reverse a string manually and then we'll show you how to do it using built-in functions. A simple way to reverse a string would be to create a new string and fill it with the characters from the original string, but backwards. To do this, we need to loop through the original string starting from the end, and every iteration of the loop we move to the previous character in the string. Here is an example:

function FirstReverse(str) { 

  var newString = "";

  // add each character to newString
  for (var i = str.length - 1; i >= 0; i--) {   
    newString = newString + str.charAt(i);
  }

  return newString;
         
}
   
FirstReverse(readline());                            
def FirstReverse(str): 

  # the easiest way to reverse a string in python is actually the following way:
  # in python you can treat the string as an array by adding [] after it and 
  # the colons inside represent str[start:stop:step] where if step is a negative number
  # it'll loop through the string backwards 
  return str[::-1]
    
print FirstReverse(raw_input())                          
def FirstReverse(str)

  newString = ""
  range = (str.length - 1)..0
  
  # loop backwards using downto method 
  (range.first).downto(range.last).each do |x|
      newString += str[x]
  end
  
  return newString
         
end

puts FirstReverse(STDIN.gets)                           

Below is code that shows how to achieve this using built-in string and array functions.

function FirstReverse(str) { 

  // first we split the string which creates an array of characters, e.g. ['c','a','t']
  // then we call the reverse function on this array 
  // and finally we turn the reversed array back into a string using the join function
  return str.split('').reverse().join('');
         
}
   
FirstReverse(readline());                            
def FirstReverse(str): 

  # reversed(str) turns the string into an iterator object (similar to an array)
  # and reverses the order of the characters
  # then we join it with an empty string producing a final string for us
  return ''.join(reversed(str))
    
print FirstReverse(raw_input())                          
def FirstReverse(str)

  # this function reverses the string in-place
  return str.reverse!
         
end

puts FirstReverse(STDIN.gets)                          
Top User Solutions
1 hyuil Challenge Score: 10 Cpp
1 cjordanball Challenge Score: 10 JavaScript
1 slavasprogis Challenge Score: 10 Python
2 Qlogin Challenge Score: 10 Python
1 emuchynski Challenge Score: 10 Ruby
3 KlaraKathrina Challenge Score: 10 Python
4 Sabin Challenge Score: 10 Python
5 Corinto Challenge Score: 10 Python
6 EmperorDuny Challenge Score: 10 Python
7 gzamfire Challenge Score: 10 Python
8 mmalkavian Challenge Score: 10 Python
2 mattlarsh Challenge Score: 10 JavaScript
3 leot75 Challenge Score: 10 JavaScript
9 lesian Challenge Score: 10 Python
4 beeboop19 Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
3 gutty333 Challenge Score: 10 Cpp
4 cdms0 Challenge Score: 10 Cpp
5 rnelson082 Challenge Score: 10 JavaScript
6 jackgeek Challenge Score: 10 JavaScript
7 escapiststupor Challenge Score: 10 JavaScript
8 nulln Challenge Score: 10 JavaScript
2 kmosier Challenge Score: 10 Ruby
10 hurgadion Challenge Score: 10 Python
3 lucanioi Challenge Score: 10 Ruby
4 Bohdan Challenge Score: 10 Ruby
9 chrigolex Challenge Score: 10 JavaScript
5 sanguookchoi Challenge Score: 10 Cpp
5 SSJ6Porfy Challenge Score: 10 Ruby
6 stevenli Challenge Score: 10 Ruby
1 mrtedjeong Challenge Score: 10 Java
10 JoanneTisch Challenge Score: 10 JavaScript
2 arjnklc Challenge Score: 10 Java
6 theBestFella Challenge Score: 10 Cpp
7 memyselfandhai Challenge Score: 10 Ruby
8 duncanbb Challenge Score: 10 Ruby
9 edwardcheng Challenge Score: 10 Ruby
10 kelaiyarao1 Challenge Score: 10 Ruby
3 moe123 Challenge Score: 10 Java
7 johne Challenge Score: 10 Cpp
1 ionutforyou Challenge Score: 10 PHP
8 Justification Challenge Score: 10 Cpp
2 tostercx Challenge Score: 10 PHP
9 Zeikar Challenge Score: 10 Cpp
4 danaksim Challenge Score: 10 Java
5 jakmaj Challenge Score: 10 Java
6 mikejagen Challenge Score: 10 Java
7 AjeethKannan Challenge Score: 10 Java
8 ph4ntom Challenge Score: 10 Java
9 binfeng Challenge Score: 10 Java
10 anta0 Challenge Score: 10 Cpp
10 trebek1 Challenge Score: 10 Java
3 longcheng Challenge Score: 10 PHP
4 teplicko Challenge Score: 10 PHP
5 richardhertzog Challenge Score: 10 PHP
6 Althalus Challenge Score: 10 PHP
7 smcdowell Challenge Score: 10 PHP
8 jetonr Challenge Score: 10 PHP
9 mihainem Challenge Score: 10 PHP
10 khattab Challenge Score: 10 PHP

Become a Premium Member

With our large collection of challengs, tutorials, and solutions, we make it easy for you to become a better coder, prepare for interviews, and learn new skills from more experienced coders.

“I love Coderbyte challenges! What an awesome way to work on your javascript and algorithm skills.“ ― Rob Rosario
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 500,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses