+ 1
Reverse a string without using strrev() in c++
5 Antworten
+ 23
Something like this?
https://code.sololearn.com/cJWnja94xBIA/?ref=app
+ 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
+ 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
+ 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.
0
In which language.
Array[] = String.split("");
Array = Array.reverse()
Array. join("" ) ;