Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer this SO answer. (I only found this SO answer after writing the code)

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer. (I only found this SO answer after writing the code)

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer. (I only found this SO answer after writing the code)

added 51 characters in body
Source Link
esote
  • 3.8k
  • 2
  • 24
  • 44

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer. (I only found this SO answer after writing the code)

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer.

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer. (I only found this SO answer after writing the code)

Source Link
esote
  • 3.8k
  • 2
  • 24
  • 44

Simply print the string backwards

This code is very, very simple, but I was wondering how it could be improved:

#include <iostream>
int main() {
 std::string input;
 std::getline(std::cin, input);
 for(int i = 1; i <= input.length(); i++){
 std::cout << input[input.length() - i];
 }
 std::cout << '\n';
 return 0;
}

This code takes a string input, and prints it backwards. For example:

This will be printed backwards.

Becomes:

.sdrawkcab detnirp eb lliw sihT

Given the simplicity of this code, is there a way to optimize it? I understand that since it is very short, it will run fast no matter what. However, I hate inefficiencies in code, and I want to help improve the way that I write code.

This is related to, but not copied directly from, this SO answer.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /