I did not get the logic of how to compare the characters of two strings.
What i was thinking was I will convert both the strings into array and then compare each element of one array with the other but how to get the remaining characters in the main string.
will this be fine or there is some simpler method to do it without converting the strings to array..
> I did not get the logic of how to compare the
> characters of two strings.
>
> What i was thinking was I will convert both the
> strings into array and then compare each element of
> one array with the other but how to get the remaining
> characters in the main string.
>
> will this be fine or there is some simpler method to
> do it without converting the strings to array..
you could use the charAt(...) method from the String class, but toCharArray() will work ok too. Why not just give it a try yourself and when you get stuck you can ask a question here.
This is what i had written but i could not figure out how the loops should be like..
public class Test_Compare
{
public static void main(String[] args)
{
String string1 = new String();
String string2 = new String();
String output = new String();
string1 = "simple";
string2 = "surge";
char[] array1 = string1.toCharArray();
char[] array2 = string2.toCharArray();
int len1 = string1.length();
int len2 = string2.length();
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
if (array2[j] == array1[i])
{
output = string1.replace(array2[j], ' ');
}
}
System.out.println(output);
}
}
}
I am getting an output like this:
imple
imple
imple
imple
imple
simpl
where am I going wrong ? and how should i correct it..
There are no occurrences of the string "surge" in the string "simple", right?
So the result should be the original string, unaltered, unless I am misunderstanding
your problem description. But your code simply compares one char from each
string and if they are equal, calls replace. Shouldn't you hold off on calling
replace until you know the entire second string is there?
And are you allowed to use other methods in the API? If so, this
algorithm could be one line long!
> > Your question is still too vague. anyway comparing
> > strings should be done with equals sign not ==.
>
> Comparing Strings should be done with the
> equals(String) method.
In the given code, array2[j] == array1[i] is comparing chars...
> > > Your question is still too vague. anyway
> comparing
> > > strings should be done with equals sign not ==.
> >
> > Comparing Strings should be done with the
> > equals(String) method.
>
> In the given code, array2[j] == array1 is
> comparing chars...
I see this; I was just correcting the poster above me, as way too many people don't know how to compare Strings.
Message was edited by:
Djaunl
Hi All,
What I want to do is I want to remove all the charcaters from the string1 which are present in both the strings.
So I thought I will first compare both the strings and and find out which charcaters of string2 are present in the string1 and then remove those from the string1.
I am still confused how to do it.. Please help.. If my approach is wrong..
You have the basic logic already, just the "How-to" is missing. Maybe you can declare another variable to store the modified string1. Use methods such as indexOf, charAt to achieve that is easier. To simplify it,
Let say string1 = "abc" and string2 = "cdr"
Then, when you access the "c" in string1 and compare it to string2...
string1.charAt(i).indexOf(string2) > -1 inside a for loop, you just skip the character in string1, otherwise just append it to another variable, say string3.
Hope this can help you.
> What I want to do is I want to remove all the charcaters from the string1 which are present in both the strings.
Are you sure? In the original post you wrote:
> I want to write a program which accepts two strings and then removes all the occurrences of the second string in the main string.
These really are completely different!
> > What I want to do is I want to remove all the
> charcaters from the string1 which are present in both
> the strings.
>
> Are you sure? In the original post you wrote:
>
> > I want to write a program which accepts two strings
> and then removes all the occurrences of the second
> string in the main string.
>
> These really are completely different!
I think both mean the same, the only difference is earlier i had written that i want to accept two strings. otherthan that its means the same ..
I do not know how to proceed further..
No, one means that if one string is "hello world" then you want to remove all occurances of the string "hello world" from another string.
The other statement is that you want to remove the occurances of all characters in "hello world from the other string. Meaning removing all 'h's, all 'e's, all 'l's, all 'w's, all 'o's,....... etc.
BIG difference!
Sorry my mistake,
But what I want to do is:
I want write a program to accept two strings and remove all the occurrences of the second string in the main string. I do not want to use any standard library functions.
Hopefully this should be clear and this is what i want to achieve out of my program, so what the best way to do it..
> But what I want to do is:
>
> I want write a program to accept two strings and
> remove all the occurrences of the second string in
> the main string. I do not want to use any standard
> library functions.
I'm confused cuz your example code tries to do the opposite: remove each character in a string from another string.
You might do well to repost your code updated to try to match this new specification...
> Let's say input string are string1 and string2
>
> String string1 = "problems";
> String string2 = "solution";
>
> then the output should be :
> string1 = "prbem";
That still doesn't say much.
So
problems -> solution = prbem
but what about:
Java -> a = Jva or Jv?
Racecar -> ca = Recar or Rer?
> I donot want to use any standard library functions...
But you were already using String's replace(...) method! What can, and what can't you use?
> If my string1 = "Java"
> And string2 = "a"
> then my output result should be "Jv"
>
> and
>
> if string1 = "Racecar "
> and string2 = "ca"
> then the output should be : Rer
>
> and initially i had used replace() , then i reailsed
> that i should not be using that...
Ok, here's a pretty simple algorithm:
public class TestCompare
{
public static void main(String[] args)
{
String 'originalString' <- the original String
String 'removeString' <- the String that contains the chars to remove
String 'newString' <- the newly formed String
LOOP 'i' from 0 to 'originalString'.length
char 'candidate' <- the char from 'originalString' at index 'i'
boolean 'isInRemoveString' <- initialize as false
LOOP 'j' from 0 to 'removeString'.length
IF 'candidate' equals the char at index 'j' from 'removeString'
set 'isInRemoveString' to true and break out of this loop
END IF
END LOOP
IF 'isInRemoveString' equals false
append 'candidate' to 'newString'
END IF
END LOOP
print 'newString'
}
}
Try to finish it.