Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. String Reverse

String Reverse

Scheduled Pinned Locked Moved Java
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V ethi
    wrote on last edited by
    #1

    Hi , I am just trying to reverse all the words of a string at their place only ex if there is a string : "vandana is a good girl" then the out put of the program should be "anadnav is a doog lrig" for that i have created a package to calculate the no of words in the string and also for calculating reverse of a string that pacage is: package p1; public class meth { public int cal(String s) { int wrd=0; for(int i=0;i=0) { for(int i=0;i

    L V 2 Replies Last reply
    0
    • V V ethi

      Hi , I am just trying to reverse all the words of a string at their place only ex if there is a string : "vandana is a good girl" then the out put of the program should be "anadnav is a doog lrig" for that i have created a package to calculate the no of words in the string and also for calculating reverse of a string that pacage is: package p1; public class meth { public int cal(String s) { int wrd=0; for(int i=0;i=0) { for(int i=0;i

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Why make it so complicated? Just read the string and then loop through the characters in reverse order and print each one. You will end up with the string reversed.

      V 1 Reply Last reply
      0
      • L Lost User

        Why make it so complicated? Just read the string and then loop through the characters in reverse order and print each one. You will end up with the string reversed.

        V Offline
        V Offline
        V ethi
        wrote on last edited by
        #3

        i need to reverse the spelling of words and need not to change their order i.e dog is an animal out should be: god si na lamina .if i use for loop then then the whole string get it'sreverse.

        L 2 Replies Last reply
        0
        • V V ethi

          i need to reverse the spelling of words and need not to change their order i.e dog is an animal out should be: god si na lamina .if i use for loop then then the whole string get it'sreverse.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then all you need to do is split the string at each wordbreak (e.g. space) and then do the same to each word.

          V 1 Reply Last reply
          0
          • V V ethi

            i need to reverse the spelling of words and need not to change their order i.e dog is an animal out should be: god si na lamina .if i use for loop then then the whole string get it'sreverse.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Take a look at the Scanner class[^], which can help in the breaking up of words.

            1 Reply Last reply
            0
            • L Lost User

              Then all you need to do is split the string at each wordbreak (e.g. space) and then do the same to each word.

              V Offline
              V Offline
              V ethi
              wrote on last edited by
              #6

              yeah same i was doing in my code can you please take a look of it

              L 1 Reply Last reply
              0
              • V V ethi

                yeah same i was doing in my code can you please take a look of it

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                As I suggested earlier, it can be simplified considerably by using the Scanner class, as in the following sample. If you do not wish to use the scanner, then you could write your own class that splits a string into a collection of tokens.

                public static void main(String\[\] args) throws Exception {
                    Scanner sc = new Scanner("Cotta and Balbus love the sweet voices of the girls");
                    while (sc.hasNext())
                    {
                        String nextWord = sc.next();
                        char c;
                        for (int i = nextWord.length() - 1; i > -1; --i)
                        {
                            c = nextWord.charAt(i);
                            System.out.print(c);
                        }
                        System.out.print(" ");
                    }
                    System.out.println(".");
                }
                
                1 Reply Last reply
                0
                • V V ethi

                  Hi , I am just trying to reverse all the words of a string at their place only ex if there is a string : "vandana is a good girl" then the out put of the program should be "anadnav is a doog lrig" for that i have created a package to calculate the no of words in the string and also for calculating reverse of a string that pacage is: package p1; public class meth { public int cal(String s) { int wrd=0; for(int i=0;i=0) { for(int i=0;i

                  V Offline
                  V Offline
                  vipul574
                  wrote on last edited by
                  #8

                  To get number of words in string u can simply split that string by space. int[] words = string.split(" "); int noofwords = words.length; after getting number of words, loop the words and use reverse logic.

                  Regards Vipul.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups