Results 1 to 5 of 5
  1. #1
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Swap characters in a string.

    For one assignment I just got today, I need to take a string and swap every other character. If the string is an odd length, leave the last character alone.

    i cant ONLY use .length(), .charAt(), and .substring methods from the string class. NOTHING ELSE!

    Examples:
    IN: laptop OUT: altppo
    IN: speaker OUT: psaeekr

    Any suggestions on how to go about this?

    Thanks a million in advance!

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    In String you are having a method toCharArray[].using this u make character array and use the swapping algorithm.


    public class SwapLetters
    {

    public SwapLetters()
    {
    }

    public static void main(String args[])
    {
    if(args.length == 0)
    {
    System.out.println("Supply a string(without whitespace) as argument");
    System.exit(0);
    }
    SwapLetters swapletters = new SwapLetters();
    if(args[0].length() % 2 == 0)
    {
    System.out.println((new StringBuilder()).append("Swapped string --> ").append(swapletters.getSwappedString(args[0])).toString());
    } else
    {
    int i = args[0].length();
    String s = args[0].substring(0, i - 1);
    System.out.println((new StringBuilder()).append("Swapped string --> ").append(swapletters.getSwappedString(s)).append( args[0].substring(i - 1)).toString());
    }
    }

    public String getSwappedString(String s)
    {
    s = s.trim();
    int i = s.length();
    char ac[] = s.toCharArray();
    char ac1[] = new char[ac.length];
    for(int j = 0; j < i; j++)
    if(j % 2 == 0)
    {
    ac1[j + 1] = ac[j];
    ac1[j] = ac[j + 1];
    }

    s = new String(ac1);
    return s;
    }
    }
    Last edited by RamyaSivakanth; 03-04-2010 at 12:11 PM.
    Ramya:cool:

  3. #3
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    I appreciate your help, however I cannot use an array. ONLY .length(), .charAt(), and .substring().

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Sdannenberg3 View Post
    For one assignment I just got today, I need to take a string and swap every other character. If the string is an odd length, leave the last character alone.

    i cant ONLY use .length(), .charAt(), and .substring methods from the string class. NOTHING ELSE!
    How inefficient, but nevertheless here goes:

    Pairwise swaps work as follows:

    1) if the length of a String s is less than two, just return the String s;

    otherwise

    2) swap the first two characters of s and concatenate the result of the pairwise swap of the rest of the String to it.

    Recursion comes in naturally here ...

    kind regards,

    Jos

  5. #5
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Try this:
    Java Code:
    package Testing;
    import java.util.*;
    public class BoredLol {
        public static void main(String[] args){
            Scanner sin = new Scanner(System.in);
            String sWord = sin.next();
            while(sWord.length()>1){
                System.out.print(sWord.charAt(1));
                System.out.print(sWord.charAt(0));
                sWord = sWord.substring(2);
            }
            System.out.println(sWord);
        }
    }

Similar Threads

  1. characters from a string into an integer
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 05-08-2012, 01:31 PM
  2. Search string for non-alphabetic characters
    By turnergirl24 in forum New To Java
    Replies: 5
    Last Post: 10-16-2009, 03:02 AM
  3. deleting characters from a String
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 08-29-2008, 01:14 PM
  4. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 09:56 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •