Results 1 to 10 of 10
  1. #1
    shf62203 is offline Member
    Join Date
    May 2017
    Location
    Lakeland, FL
    Posts
    5
    Rep Power
    0

    Default Trying to replace string characters using index from another string

    I have one string that's value is known (str1 for example), and another that is read in from a file (str2). I need to read through str2 and replace specific characters using the index of str1. So I'm making a monoalphabetic cipher. I've already read in my keyword from a text file, appended to the beginning of str1, then removed redundant letters from str1. Now I want to:

    Java Code:
    str1 = "ZYXWVUTSRQPONMLKJIHGFEDCBA" 
    str2 = "SOME SENTENCE"
    I want to:

    read str2 and:

    for every 'A' found, replace with str1 (index 0)
    for every 'B' found, replace with str2 (index 1)
    and so on until
    for every 'Z' found, replace with str2 (index 25)

    return final result to str3

    I want my end result to be:

    Java Code:
    str3 = "HLNV HVMVGEMXV"
    Do I have to convert the strings to char arrays to do something like this?
    Last edited by shf62203; 05-01-2017 at 02:56 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to replace string characters using index from another string

    There are lots of ways to do it. But the most straight forward is to iterate over a character array. I would actually
    build the new string using an instance of the StringBuilder class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    shf62203 is offline Member
    Join Date
    May 2017
    Location
    Lakeland, FL
    Posts
    5
    Rep Power
    0

    Default Re: Trying to replace string characters using index from another string

    Ok, I got the char arrays set for both strings. What should I look for to show me how to replace a specific char in on array with a specific char from another array?

  4. #4
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Trying to replace string characters using index from another string

    As I understood it, you want to replace one character in a string by another, taken from another string..

    So if the string is "Hello" and want to replace the 'e' with 'a'
    Why don't you use substring to split the string to two and then add 'a' in between like so:

    public static void main (String args[]){
    String str1 = "Hello";
    String result= "";
    int replaceIndex= 1;

    String leftPart = str1.substring(0,replaceIndex);
    String rightPart = str1.substring(replaceIndex+1,str1.length());
    result+=leftPart+'a'+rightPart;
    System.out.println(result);
    }

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to replace string characters using index from another string

    That doesn't really solve the problem the OP asked. For every A, B, C, etc, the OP wants to replace the contents with Z Y X, etc.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to replace string characters using index from another string

    Ok, you can actually do it the way you want but it is not as straight forward as I am going to suggest. Notice you only gave one
    half of the cipher. But what about the first have. Here is an example:

    String plaintest = "ABCDEFGHIJLKMNOPQRSTUVWXYZ";
    String ciphertext = "ZYXWVUTSRQPONMLKJIHGFEDCBA";

    Now, wouldn't it be easier that for every character you have your plaintext message of SOME MESSAGE,
    you find the index of the plaintext letter and replace it with the ciphertext one at the same location.

    Then you can totally jumble your ciphertext string above and come up multiple 'encryptions.'

    Otherwise you need to use the derived character value from SOME MESSAGE, say 'S' and use its
    inherent value to generate a specific index so you can pull the character out of the ciphertext String.
    It is doable but not as easy and not as flexible.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    shf62203 is offline Member
    Join Date
    May 2017
    Location
    Lakeland, FL
    Posts
    5
    Rep Power
    0

    Default Re: Trying to replace string characters using index from another string

    my full ciphertext has the keyword (from file) appended to the front. So my code is built so that it can handle any keyword (if the keyword changes inside the "keyword.txt" file. I took the newCipher string and converted to a charArray, and also read in my plaintext from file and converted it to a charArray. I found example on how to replace one character in an array with another character, but could not find how to replace an index's inherent value with a character from another array (I would probably take the index of the newCipher array here). I think I'm using my terms correctly, if not, I'm sorry for any confusion. My assignment has already been turned in, but I'd still like to solve my problem and learn something.

  8. #8
    shf62203 is offline Member
    Join Date
    May 2017
    Location
    Lakeland, FL
    Posts
    5
    Rep Power
    0

    Default Re: Trying to replace string characters using index from another string

    So your suggestion is something like:

    find plaintext character 'A' index #, then replace that index # with ciphertext index 0? That is what I'm trying to do but not sure how. I converted to char arrays but not sure that's how I want to do it. I like working with strings better I think.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Trying to replace string characters using index from another string

    Constantly replacing a string with a new String will work. But also building a new String on the fly will work using Stringbuilder and will be more efficient. But
    use the method you are most comfortable with.

    So let's say the first character is an 'S'. that S has an integer value of 83. The letter 'A' starts at 65. And you can do math with char values. So
    'S' - 'A' is equal to 18. So that number can be used to index into your reversed alphabet to get the replacement character. You just need to "automate" it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    shf62203 is offline Member
    Join Date
    May 2017
    Location
    Lakeland, FL
    Posts
    5
    Rep Power
    0

    Default Re: Trying to replace string characters using index from another string

    That makes sense. I like the concept of stringbuilder so I think I'll work with it until I understand it. Time for some research and tutorials. Thanks for the suggestions!

Similar Threads

  1. String.replace ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-22-2011, 03:04 AM
  2. Using regex to replace characters in a string
    By DC200 in forum New To Java
    Replies: 7
    Last Post: 10-13-2010, 02:35 PM
  3. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 10:14 AM
  4. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Tags for this Thread

Posting Permissions

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