I managed to split the string but how can I swap h with t and t with h in second half of string ?
public class Tollring3 {
public static void main(String args[])
{
String base = "httthhhtth";
int half = base.length()/2;
// int half = base.length() % 2 == 0 ? base.length()/2 : base.length()/2 1;
String first = base.substring(0, half);
String second = base.substring(half);
System.out.println("Actual String :- " base);
System.out.println(first);
System.out.println(second);
char[] c = second.toCharArray();
// Replace with a "swap" function, if desired:
char temp = c[0];
c[0] = c[1];
c[1] = temp;
String swappedString = new String(c);
System.out.println(swappedString);
}
}
How to swap h with t and t with h in second half of string
CodePudding user response:
This program splits a string into two halves, then swaps the first and second characters of the second half. public class Tollring3 {
public static void main(String args[])
{
String base = "httthhhtth";
int half = base.length()/2;
// int half = base.length() % 2 == 0 ? base.length()/2 : base.length()/2 1;
String first = base.substring(0, half);
String second = base.substring(half);
System.out.println("Actual String :- " base);
System.out.println(first);
System.out.println(second);
char[] c = second.toCharArray();
// Replace with a "swap" function, if desired:
char temp = c[0];
c[0] = c[1];
c[1] = temp;
String swappedString = new String(c);
System.out.println(swappedString);
}
}
CodePudding user response:
To do the replacement you need to iterate on the chars and use if
to test what char it is
String base = "httthhhtth";
System.out.println(base);
char[] c = base.toCharArray();
for (int i = 0; i < c.length; i) {
if (i >= c.length / 2) { // check for change only in second half
if (c[i] == 'h') {
c[i] = 't';
} else if (c[i] == 't') {
c[i] = 'h';
}
}
}
String swappedString = new String(c);
System.out.println(swappedString);
httthhhtth
httthtthht
CodePudding user response:
You can define the swap
function as follows:
String swap(String str) {
char[] arr = str.toCharArray();
char[] result = new char[arr.length];
for (int i = 0; i < arr.length; i )
result[i] = arr[i] == 'h' ? 't' : arr[i] == 't' ? 'h' : arr[i];
return new String(result);
}
In the function, we have
- split the given string (second half) into a
char
array,arr
. - created another
char
array,result
of the same size. - iterates
arr
to store each character fromarr
intoresult
while swappingh
andt
with each other. - finally returned a new
String
created out ofresult
.
Full demo:
public class Main {
public static void main(String args[]) {
String base = "httthhhtth";
int half = base.length() / 2;
// int half = base.length() % 2 == 0 ? base.length()/2 : base.length()/2 1;
String first = base.substring(0, half);
String second = base.substring(half);
System.out.println("Actual String :- " base);
System.out.println(first);
System.out.println(second);
String swappedString = swap(second);
System.out.println(swappedString);
}
static String swap(String str) {
char[] arr = str.toCharArray();
char[] result = new char[arr.length];
for (int i = 0; i < arr.length; i )
result[i] = arr[i] == 'h' ? 't' : arr[i] == 't' ? 'h' : arr[i];
return new String(result);
}
}
Output:
Actual String :- httthhhtth
httth
hhtth
tthht