6

my assignment question is like that

Write a program which prints the letters in a char array in reverse order using

void printReverse(char letters[], int size);

For example, if the array contains {'c', 's', 'c', '2', '6', '1'} the output should be "162csc".

I tried, but I don't know what it means

void printReverse(char letters[], int size);

I did this but there's a problem with calling the method "printReverse" into the main method

import java.util.Arrays;
import java.util.Collections;
 
public class search {

    public static void main(String[] args) {          
 
        char[] letters = {'e', 'v', 'o', 'l', '4'};
        printReverse();

    }

    public void printReverse(char[] letters, int size) {
    
        for (int i = letters.length-1; i >= 0 ; i--) {
        System.out.print(letters[i]);
    }
}
djm.im
  • 3,295
  • 4
  • 30
  • 45

10 Answers10

26

You can make use of StringBuilder#reverse() method like this:

String reverse = new StringBuilder(new String(letters)).reverse().toString();
anubhava
  • 761,203
  • 64
  • 569
  • 643
7

I believe what you wrote is the signature of the method you have to create.

public void printReverse(char[] letters, int size){
   //code here
}

You would have to iterate the array and print what it contains backwards. Use a reverse "for loop" to go through each item in "letters". I'll let you combine these yourself as it's an assignment. Here's an example of a for loop:

for (int i = array.length-1; i >= 0 ; i--){
    System.out.print(array[i]);
}
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63
  • I did like this .. import java.util.Arrays; import java.util.Collections; public class search { public static void main(String[] args) { char[] letters = {'e', 'v', 'o', 'l', '4'}; printReverse(); } public void printReverse(char[] letters, int size){ for (int i = letters.length-1; i >= 0 ; i--){ System.out.print(letters[i]); } } } –  Dec 11 '12 at 20:45
  • It seems like it would have to be "printReverse(letters,letters.length);" - you have to pass the variables to your method. Other than that it looks okay. I do wonder what the "size" parameter is for - odd that there is no explaination for that. – Peter Rasmussen Dec 11 '12 at 20:53
  • Also I think that your method is missing the keyword "static". - as in "public static void printReverse" – Peter Rasmussen Dec 11 '12 at 21:04
  • u r right it worked , I was forgetting "static" in the method :D thank u so much man :) –  Dec 11 '12 at 21:10
6

This should take about 6 ms. It reverses a char array "in-place" before printing.

public static void reverseString(char[] s) {
    int len = s.length;

    if (len == 0)
        return;

    for (int i=0; i < (len/2); i++)
    {
        char l = s[i];
        s[i] = s[len-i-1];
        s[len-i-1] = l;
    }

    System.out.println(s);
}
Prodigy
  • 380
  • 3
  • 9
3

`

//not only prints the reverse order, but creates new char array with chars in desired order
char[] letters = {'e', 'v', 'o', 'l', '4'};
int i = letters.length - 1, j = 0;
char[] let = new char[letters.length];
while(i >= 0){
     let[j] = letters[i];
     i--;
     j++;
}
for (char c : let){
     System.out.print(c);
}

`

output: 4love

0

void printReverse(char letters[], int size) is the signature of the function that you have to do. E.g.

void printReverse(char letters[], int size) {
//your code goes here
}

and call it from your main window with the parameters.

Laf
  • 7,965
  • 4
  • 37
  • 52
Luis Tellez
  • 2,785
  • 1
  • 20
  • 28
0

Man you code is right except some minor changes in the main method and in the loop and the method has to be static.

The signature printReverse(char[] letters, int size) means that when you call it, you have to pass char array and the size of the array

Try the following

import java.util.Arrays;
import java.util.Collections;

public class search {

    public static void main(String[] args) {          

   char[] letters = {'e', 'v', 'o', 'l', '4'};

   printReverse(letters,5);

}

public static void printReverse(char[] letters, int size){

    for (int i = size-1; i >= 0 ; i--)
    {

     System.out.print(letters[i]);
    }
  }

 }
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • thank a lot man u r right , but what is number 5 refers to in "printReverse(letters,5);" ?? –  Dec 11 '12 at 21:07
  • The number 5 is the size of the array in your example :) but you can also do like printReverse(letters, letters.length) – Dan Hunex Dec 11 '12 at 21:30
  • @DanHunex you can also shorten it even more by removing the `int size` and in your array writing letters.length – Zoe May 07 '17 at 19:39
0
public void printReverse(char[] word){

reverseWordMaxIndex = word.length -1;

char[] reverseWord = new char(reverseWordMaxIndex + 1)

for ( int i= 0; i < word.length; i++){

reverseWord[reverseWordMaxIndex-i] = word[i];

}

for( int i= 0; i < reverseWord.length; i++){
  System.out.println(reverseWord[i]);
   }

}
kenadet
  • 245
  • 5
  • 13
0

This is a class which reveres the character array using recursion.

public class ReverseString {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String str = new String("Hello");
        char[] strcharArray = str.toCharArray();
        printReverse(strcharArray);

    }

    private static void printReverse(char [] str) {
          helper(0, str);
        }
        private static void helper(int index, char [] str) {
          if (str == null || index >= str.length) {
            return;
          }
          helper(index + 1, str);
          System.out.println(str[index]);
        }
}
Vinay John
  • 990
  • 12
  • 13
0
#include<stdio.h>
int main()
  {
int i;
int temp;
int end;
int n=6;
int ar[6]={1, 2,3 , 4, 5, 6};
end=n-1;
printf("%d\n",end );
for(i = 0; i < 6; ++i)
 {
    temp=ar[i];
    ar[i]=ar[end];
    ar[end]=temp;
    end--;
    if(sizeof ar[6]/2==end){
        break;
    }
 } 
 printf("\n");
 for (int i = 0; i < 6; ++i)
 {
    printf("%d",ar[i] );
 }
 return 0;
}

/one can write same for any type of array/

-1
import java.util.Scanner;

public class StringReverse {

    static String input;
    void completeReverse(){
        char[] ch = StringReverse.input.toCharArray();
        for(int i =ch.length-1;i>=0;i--){
            System.out.print(ch[i]);
        }
    }
    void partialReverse(){
        String charOfSplit =" ";
        // splitting and creating array on space character.
        String[] split =StringReverse.input.split(charOfSplit);
        //using loop on each index of split characters.
        for(int i=0; i<split.length;i++){
           // System.out.println(split[i]);
            //breaking individual index in char array with different index value.
            char[] indexedValue = split[i].toCharArray();
            for(int j=indexedValue.length-1;j>=0;j--){
                //printing that char array reverse order
                System.out.print(indexedValue[j]);
            }
            // giving spaces for another index value after space given by user.
            System.out.print("  ");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringReverse sr =new StringReverse();
        System.out.print("Enter the String: ");
        //trimming every space before and after the string.
        StringReverse.input = sc.nextLine().trim();
        System.out.print("Complete Reverse: ");
        sr.completeReverse();
        System.out.println();
        System.out.print("Partial Reverse: ");
        sr.partialReverse();
    }
}
Tom
  • 4,070
  • 4
  • 22
  • 50