Wednesday, 7 August 2013

How can I write a function boolean succeeds(char a, char b, String s)? using s.equals(""), s.charAt(0), s.substring(1)

How can I write a function boolean succeeds(char a, char b, String s)?
using s.equals(""), s.charAt(0), s.substring(1)

I am trying to write a function that takes a string s and returns true if
every occurrence of the character b is always succeeded by the character
a, and false otherwise. I have tried:
boolean succeeds(char a, char b, String s) {
boolean to_return = true;
boolean seen_a = false;
while(true) {
if (s.equals("")) return to_return;
char c2 = s.charAt(0);
if (c2 == a) seen_a = true;
if (c2 == b) {
if (!seen_a) return false;
}
s = s.substring(1);
}
}
}
I think I have the right idea. But I don't know how to put it together.

No comments:

Post a Comment