# Count Singletons in a String

• April 12th, 2014, 03:18 AM
NorrinGalan
Count Singletons in a String
For a problem, I have to write a code that counts the number of singletons, stored in the variable n, in a string sequence. A singleton is a word in a sequence that does not appear before or after itself in the sequence.

I initialized the count variable n with 2, since the first and last tokens can only have one
copy before and after each other respectively. I divided the string sequence into tokens by splitting it with respect to the empty spaces between them.

Here is my code.

Code :

```String string = " "; string = stdin.next();   String[] tokens = string.split(" ");   n = 2;   for (int i = 1; i < tokens.length()-1; i++){ while (!(tokens[i].equals("xxxxx"))){ if (!(tokens[i].equals(tokens[i-1]) && !(tokens[i].equals(tokens[i+1]))) n++; } }```
• April 12th, 2014, 07:10 AM
Norm
Re: Count Singletons in a String
Do you have a question?

Also posted at: http://forums.devshed.com/java-help-...ng-960893.html
• April 12th, 2014, 02:08 PM
NorrinGalan
Re: Count Singletons in a String
Quote:

Originally Posted by Norm
Do you have a question?

Also posted at: Singletons in a string

Yes, I know, I posted the same question on two different forums. I hope that this is not against the rules of the forums.
• April 12th, 2014, 02:28 PM
Norm
Re: Count Singletons in a String
Did you have any specific questions?
• April 12th, 2014, 03:59 PM
GregBrannon
Re: Count Singletons in a String
What's your definition of 'singleton?' That term has a specific meaning in programming, and I don't think that's what you mean.
• April 12th, 2014, 06:21 PM
NorrinGalan
Re: Count Singletons in a String
Quote:

Originally Posted by GregBrannon
What's your definition of 'singleton?' That term has a specific meaning in programming, and I don't think that's what you mean.

Well, the problem specification states that: "A singleton is a word in a sequence that does not appear before or after itself in the sequence."

--- Update ---

Quote:

Originally Posted by Norm
Did you have any specific questions?

Yes, I would like to know if someone can help me figure out why my code is not working. It is suppose to find singletons in a string, and count the number of time the singletons appear.
• April 12th, 2014, 06:42 PM
Norm
Re: Count Singletons in a String
See the other site where this is posted.