Write a function that takes an input parameter as a string and return the alternate words in it with “abc”. Words are separated by dots. Note: Avoid using inbuilt functions
Input: "i.like.this.program.very.much" Output: "i.abc.this.abc.very.abc"
CodePudding user response:
Would something like this work?
public String func(String s) {
String[] arr = s.split("\\.");
for (int i = 0; i < arr.length; i ) {
if (i % 2 == 1)
arr[i]= "abc";
}
String rString = "";
for (int i = 0; i < arr.length - 1; i ) {
rString = arr[i];
rString = ".";
}
rString = arr[arr.length - 1];
return rString;
}
Not my most efficient work, but it was what I came up with on the spur of the moment.
CodePudding user response:
Not sure what you exactly mean by avoiding built in methods, but this should work:
public String whyNot(String s) {
String[] sA = s.split("\\.");
String newS = sA[0];
for (int i = 1; i < sA.length; i=i 2) {
newS = ".abc." sA[i];
}
return newS;
}