Can anyone please translate this java code into c++?

working java code:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

class decode {

public static char[] charArray;

public decode(String word) {
charArray = word.toCharArray();
decode_word(charArray.length);
}

public void changeOrder(int newsize) {
int j;
int pointAt = charArray.length - newsize;
char temp = charArray[pointAt];

for (j = pointAt + 1; j < charArray.length; j++) {
charArray[j - 1] = charArray[j];
}

charArray[j - 1] = temp;

}

public void decode_word(int newsize) {
if (newsize == 1) {
return;
}
for (int i = 0; i < newsize; i++) {
decode_word(newsize - 1);
if (newsize == 2) {
display();
}
changeOrder(newsize);
}
}

public void display() {
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]);
}
System.out.println();
}

public static void main(String args[]) {
String input;
System.out.println("enter the 3 letter word");
Scanner sc=new Scanner(System.in);
input=sc.next();
decode test1 = new decode(input);

}
}
Nope.

Life is really, truly, honestly, ever so much easier if you work with writing your own code instead of trying to understand some other random person’s on the internet. (If I am reading that code correctly, it reverses (?) a word the least straight-forward way possible.)
Came back to add more: if you are trying to reverse a string, the fastest, easiest way to do that is to simply:

  ➀ swap the first and last characters in the string
  ➁ repeat for the characters in the middle

For example:

    a b c d e
    ↓       ↓
    e b c d a
      ↓   ↓
    e d c b a
        ↓
    e d c b a

Code to do this takes a single loop and two indices: one to the first character to swap and one to the last. Loop terminates when first ≥ last.

Hope this helps.
To reverse a string in c++, just use std::reverse()

http://www.cplusplus.com/reference/algorithm/reverse/
The code prints the permutations of the input string. I don't know why it specifically asks for a 3 letter word since it seems to work with any size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;

void display(const string& s) {
    for (size_t i = 0; i < s.size(); i++) cout << s[i];
    cout << '\n';
}

void changeOrder(string& s, int newsize) {
    int pointAt = s.size() - newsize;
    char temp = s[pointAt];
    int j = pointAt + 1;
    for ( ; j < int(s.size()); j++) s[j - 1] = s[j];
    s[j - 1] = temp;
}

void decode_word(string& s, int newsize) {
    if (newsize == 1) return;
    for (int i = 0; i < newsize; i++) {
        decode_word(s, newsize - 1);
        if (newsize == 2) display(s);
        changeOrder(s, newsize);
    }
}

void decode_word(string s) {
    decode_word(s, s.size());
}

int main() {
    string s;
    cout << "enter the 3 letter word: ";
    getline(cin, s);
    decode_word(s);
}


1
2
3
4
5
6
7
8
Sample run:
enter the 3 letter word: abc
abc
acb
bca
bac
cab
cba

Well if it's just the permutations required, then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
#include <string>

int main() {
	std::string mystr;
	size_t cnt {};

	std::cout << "Enter the word: ";
	std::getline(std::cin, mystr);

	std::sort(mystr.begin(), mystr.end());

	do {
		std::cout << mystr << '\n';
	} while (++cnt, std::next_permutation(mystr.begin(), mystr.end()));

	std::cout << cnt << " permutations\n";
}



Enter the word: abc
abc
acb
bac
bca
cab
cba
6 permutations

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either


Can anyone please translate this java code into c++?

Makes me think someone is requesting others do some work. Certainly not for free, I hope.
translating is almost always a failure.
- if the code is really small and simple, its better to rewrite it using the other language's strong points and styles.
- if the code is larger and complicated, the task quickly becomes impossible as you run into things that can't be done or can't be done the same way in the other language and require exotic work-arounds, for example overloaded operators in c++ going back to java would be hair-pulling. A very simple (half page) CRC in c++ turned into a fair hassle in java due to its screwy shoehorned in after the fact support for unsigned/bitwise math.

And you can see that is what happened here -- the help is all do-over correctly in c++ rather than attempts to 'translate' it.
Topic archived. No new replies allowed.