+ 1

Reverse a string without using strrev() in c++

22nd Mar 2018, 2:14 PM
Mayank
Mayank - avatar
5 Respuestas
22nd Mar 2018, 2:24 PM
Frost
Frost - avatar
+ 4
My apologies on the delay; I'm at work. There are so many ways of going about this, but here is a way that's consistent with the example I posted in Java. As mentioned before, exact same concept of simply working backwards and storing the values in a new variable. https://code.sololearn.com/cCuCwQ4vd77A/#cpp #include <iostream> using namespace std; #include <stdio.h> #include <string.h> int main() { char forward[] = "This is my string!"; char backward[strlen(forward)] = ""; int j = 0; for(int i = strlen(forward); i >= 0; --i){ backward[j] += forward[i]; cout << backward[j]; j++; } return 0; } :::: OUTPUT :::: !gnirts ym si sihT
22nd Mar 2018, 2:48 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
Sorry, I did this before you edited your post to let us know which language. However, exact same concept applies; I'll translate it to C++ in a moment when I get a chance. JAVA: https://code.sololearn.com/cVD4wXKq3g2y/#java public class Program { public static void main(String[] args) { String forward = "This is my string!"; String backward = ""; for(int i = forward.length()-1; i >= 0; --i){ backward += forward.charAt(i); } System.out.println(backward); } } :::::: OUTPUT :::::: !gnirts ym si sihT
22nd Mar 2018, 2:21 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
Use a loop and iterate backwards through it, storing each value into a new string. Then print the new string; it'll be the previous string but backwards.
22nd Mar 2018, 2:15 PM
Fata1 Err0r
Fata1 Err0r - avatar
0
In which language. Array[] = String.split(""); Array = Array.reverse() Array. join("" ) ;
22nd Mar 2018, 2:17 PM
josh mizzi
josh mizzi - avatar