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
3 KlaraKathrina Challenge Score: 10 Python
1 junjunparkpark Challenge Score: 10 JavaScript
2 cjordanball Challenge Score: 10 JavaScript
4 Corinto Challenge Score: 10 Python
5 EmperorDuny Challenge Score: 10 Python
6 gzamfire Challenge Score: 10 Python
7 mmalkavian Challenge Score: 10 Python
3 GarenLiang Challenge Score: 10 JavaScript
2 lokeshmogra Challenge Score: 10 Cpp
4 beeboop19 Challenge Score: 10 JavaScript
3 cdms0 Challenge Score: 10 Cpp
5 jackgeek Challenge Score: 10 JavaScript
6 escapiststupor Challenge Score: 10 JavaScript
8 hurgadion Challenge Score: 10 Python
4 gutty333 Challenge Score: 10 Cpp
9 scates Challenge Score: 10 Python
10 ricky0309 Challenge Score: 10 Python
7 chrigolex Challenge Score: 10 JavaScript
2 Bohdan Challenge Score: 10 Ruby
5 sanguookchoi Challenge Score: 10 Cpp
8 mattlarsh Challenge Score: 10 JavaScript
3 zestybmac Challenge Score: 10 Ruby
9 deuzen Challenge Score: 10 JavaScript
1 arjnklc Challenge Score: 10 Java
10 SuprDewd Challenge Score: 10 JavaScript
6 theBestFella Challenge Score: 10 Cpp
4 toasterbob Challenge Score: 10 Ruby
5 SSJ6Porfy Challenge Score: 10 Ruby
6 duncanbb Challenge Score: 10 Ruby
7 kelaiyarao1 Challenge Score: 10 Ruby
2 mrtedjeong Challenge Score: 10 Java
8 mienaineko Challenge Score: 10 Ruby
7 johne Challenge Score: 10 Cpp
9 randyjap Challenge Score: 10 Ruby
10 johncoleman83 Challenge Score: 10 Ruby
1 ionutforyou Challenge Score: 10 PHP
8 Justification Challenge Score: 10 Cpp
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.

“Daniel - Thanks so much for making Coderbyte! We still recommend it to people.” ― Shawn Drost
  • 200+ Coding Challenges
  • Mock Interview Questions
  • 500,000+ Code Solutions
  • Algorithm Tutorials
  • Interview Prep Courses