-1

I'm not allowed to use the replace() method and I basically have to have a part of my program that does this:

-Ask the user to type in a sentence to be manipulated. -Then ask the user to enter which character to be replaced (i.e. "e" from "hello"). -Then ask which character they would like to it to be replaced with.

After it does all of this, it will output to the screen the manipulated string.

EXAMPLE:

  • console: Please input a string:
  • user: Hi there
  • console: please enter the character to be replaced:
  • user: e
  • console: please enter a character to replace it with:
  • user: l
  • console: the new string is: Hi thlrl

I basically am able to do this, using this code:

    int count = 1;
    char replaceAll = 'a';
    char newChar = 'b';
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the string to be manipulated");
    String sentence = keyboard.nextLine();

        int count = 0;
        System.out.println("Enter the character to replace");
        replaceAll = keyboard.nextLine().charAt(0);
        System.out.println("Enter the new character");
        newChar = keyboard.nextLine().charAt(0);
        System.out.print("The new sentence is: ");

        for(int c = 0; c < sentence.length(); c++){
            if(sentence.charAt(c) == replaceAll)
                System.out.print(newChar);
            else
                System.out.print(sentence.charAt(c));

MAIN PROBLEM: The only problem is that this won't save the manipulated string as a new string. It will only print the letters in the correct order for output (it prints one character at a time, not one string). I need the manipulated string (i.e. Hi thlrl) to be saved as a new string so that I can do more manipulations afterwards to that new string.

Ravi
  • 30,829
  • 42
  • 119
  • 173
David -
  • 41
  • 2
  • 4

5 Answers5

1

You could declare a char[] as per your sentence size and then populate it in loop.

  char[] ch = new char[sentence.length()];

  for(int c = 0; c < sentence.length(); c++)
   {
        if(sentence.charAt(c) == replaceAll)
        {
            System.out.print(newChar);
            ch[c]=newChar;
        }
        else
        {
            System.out.print(sentence.charAt(c));
            ch[c]=sentence.charAt(c);
         }
     }         

Other possible solution is, you could create char[] using toCharArray()

  char[] ch = sentence.toCharArray();

  for(int c = 0; c < sentence.length(); c++)
   {
        if(sentence.charAt(c) == replaceAll)
        {
            ch[c]=newChar;
        }
   }

And, create String using char[].

  String mystring = new String(ch);
Ravi
  • 30,829
  • 42
  • 119
  • 173
1

If it is Stringbuffer/StringBuilder then more simple is to using setCharAt

   StringBuilder sentenceStr = new StringBuilder(sentence); 
   sentenceStr.setCharAt(c, newChar);
forqzy
  • 389
  • 2
  • 11
0

You can use split to cut the string into pieces, on every occurrence of the character you wish to replace, then join to put it back together. For example,

String.join("l", "Hello there".split("e", -1));

evaluates to "Hllllo thlrl".

Note that the -1 is important - it stops the final element from being discarded if it's the one you're trying to split on.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

You could initialize an empty string and as you loop across your sentence string, you can build up the new empty string like this:

String result = "";
for(int c = 0; c < sentence.length(); c++) {
    if(sentence.charAt(c) == replaceAll) {
        result += newChar;
    } else {
       result += sentence.charAt(c);
    }
 }
System.out.println(result);
0

/* char 'o' replace with char 'd' without replace function */

    String oldString = "welcome mojo";

    String[] StringArray = oldString.split("");

    String newString = "";

    int count = StringArray.length;

    for (int i = 0; i < count; i++) {

        if (StringArray[i].equalsIgnoreCase("o")) {

            StringArray[i] = "d";
        }

        newString = newString + StringArray[i];

    }
    System.out.println(newString);