Salesforce Interview Question
Testing / Quality AssurancesCountry: United States
I know my code looks a bit weird. I wanted to play with arrays using arraycopy so just tried to solve this problem using that. And it worked. Any suggestions or improvements will be helpful.
import java.util.*;
import java.lang.*;
class SubStringWithoutCharacter
{
public static void main(String arg[])
{
String s1="google";
char c='g';
char[] str=s1.toCharArray();
int i=0;int j=0;
int count=0;
char[] temp=new char[str.length];
int mtemp=str.length;
while(mtemp!=0)
{
if(c == str[i])
{
count++;
i++;
if(count<=1)
{
temp=new char[str.length-count];
System.arraycopy(str,i,str,0,str.length-i);
}
else
{
j++;
System.out.println("i value " +i+" char "+str[i]);
System.out.println("Destination index "+j);
System.out.println("Copying Length "+(str.length - i-count+1));
if(str.length-i-count+1!=0)
System.arraycopy(str,i,str,i-count+j,str.length - i-count+1);
else
break;
}
System.out.println("Copying");
for(int k=0;k<str.length;k++)
{
System.out.print(str[k]);
}
System.out.println();
}
else
{
i++;
}
mtemp--;
System.out.println("Pass "+mtemp);
}
String result="";
for(int cm=0;cm<str.length-count;cm++)
result=result+str[cm];
System.out.println("Final output "+result);
}
}
public class Main {
public static void main(String[] args) {
String str = "google";
char remove = 'g';
StringBuilder builder = removeChar(str, remove);
System.out.println(builder);
}
public static StringBuilder removeChar(String str, char remove) {
StringBuilder builder = new StringBuilder();
for (char ch:str.toCharArray()) {
if (ch != remove) {
builder.append(ch);
}
}
return builder;
}
}
public class class1 {
public static void main(String[] args) {
System.out. println ( remove("google", 'g'));
}
public static String remove (String value, char string){
String newStr = "";
char [] array1 = value.toCharArray();
for (int i=0; i< value.length(); i++ )
{
if ( array1[i] != string)
{
newStr = newStr + array1[i];
}
}
return newStr;
}
}
public static string ReplaceChar(string s, char c)
{
if (s == null) return null;
if (c == null) return s;
StringBuilder rStr = new StringBuilder(s.Length);
for (int i = 0; i < s.Length; ++i)
{
if (s[i] != c)
rStr[i] = s[i];
}
return rStr.ToString();
}
- pacman February 13, 2013