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 Qlogin Challenge Score: 10 Python
2 Sabin Challenge Score: 10 Python
1 emuchynski Challenge Score: 10 Ruby
2 junjunparkpark Challenge Score: 10 Ruby
1 cjordanball Challenge Score: 10 JavaScript
3 Corinto Challenge Score: 10 Python
4 EmperorDuny Challenge Score: 10 Python
5 gzamfire Challenge Score: 10 Python
6 mmalkavian Challenge Score: 10 Python
2 GarenLiang Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
3 cdms0 Challenge Score: 10 Cpp
3 beeboop19 Challenge Score: 10 JavaScript
4 jackgeek Challenge Score: 10 JavaScript
5 escapiststupor Challenge Score: 10 JavaScript
7 hurgadion Challenge Score: 10 Python
8 scates Challenge Score: 10 Python
9 ricky0309 Challenge Score: 10 Python
6 chrigolex Challenge Score: 10 JavaScript
10 philtroin Challenge Score: 10 Python
4 sanguookchoi Challenge Score: 10 Cpp
7 mattlarsh Challenge Score: 10 JavaScript
8 deuzen Challenge Score: 10 JavaScript
1 arjnklc Challenge Score: 10 Java
9 SuprDewd Challenge Score: 10 JavaScript
5 gutty333 Challenge Score: 10 Cpp
10 gladysum Challenge Score: 10 JavaScript
6 theBestFella Challenge Score: 10 Cpp
3 Bohdan Challenge Score: 10 Ruby
4 toasterbob Challenge Score: 10 Ruby
5 duncanbb Challenge Score: 10 Ruby
6 kelaiyarao1 Challenge Score: 10 Ruby
2 mrtedjeong Challenge Score: 10 Java
7 mienaineko Challenge Score: 10 Ruby
7 johne Challenge Score: 10 Cpp
8 randyjap Challenge Score: 10 Ruby
9 johncoleman83 Challenge Score: 10 Ruby
1 ionutforyou Challenge Score: 10 PHP
8 Justification Challenge Score: 10 Cpp
10 ironfingers Challenge Score: 10 Ruby
2 tostercx Challenge Score: 10 PHP
9 Zeikar Challenge Score: 10 Cpp
3 danaksim Challenge Score: 10 Java
4 jakmaj Challenge Score: 10 Java
5 mikejagen Challenge Score: 10 Java
6 ph4ntom Challenge Score: 10 Java
7 binfeng Challenge Score: 10 Java
10 anta0 Challenge Score: 10 Cpp
8 trebek1 Challenge Score: 10 Java
9 k8conant Challenge Score: 10 Java
10 nicklivens Challenge Score: 10 Java
3 longcheng Challenge Score: 10 PHP
4 teplicko Challenge Score: 10 PHP
5 Althalus Challenge Score: 10 PHP
6 smcdowell Challenge Score: 10 PHP
7 jetonr Challenge Score: 10 PHP
8 mihainem Challenge Score: 10 PHP
9 khattab Challenge Score: 10 PHP
10 devilin 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.

“If you're able to solve Medium Coderbyte problems and have a good understanding of web development basics [...] then you are probably ready for admissions at the top schools.” ― Huntly Mayo-Malasky
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 500,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses