0

I have to do this

INPUT:SUCCESS

OUTPUT:
C - 2
E - 1
S - 2
U - 1

I am aware this question has been asked here: How to count frequency of characters in a string?

But my query is that I need to solve the problem by only using two outside classes Scanner or StringTokenizer. I cannot use any other library.

PS:The characters have to be in alphabetical order

Community
  • 1
  • 1
SamHat
  • 29
  • 3
  • Nep I have an exam tomorrow so it is a past years question which came in the paper – SamHat Dec 06 '15 at 20:50
  • The problem is: the solution so simple that you should not even use a String Tokenizer! – user3437460 Dec 06 '15 at 20:50
  • Even the link pasted by Tunaki are showing over convoluted solutions to this simple task :P – user3437460 Dec 06 '15 at 20:53
  • So please can you give me a hint for an easier solution.Just a hint to guide me in the right direction. – SamHat Dec 06 '15 at 20:54
  • @SamHat The hint is, you only need an array. The entire logic is as short as 4 lines. – user3437460 Dec 06 '15 at 20:56
  • I tried but just dont know how to delete duplicates – SamHat Dec 06 '15 at 20:57
  • @SamHat There is no need to delete duplicates, no need to sort. – user3437460 Dec 06 '15 at 20:57
  • You shouldn't be using [`StringTokenizer`](http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) at all (javadoc: *"`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code"*). Here is a link to an answer that does what you want, and also prints the result nicely: http://stackoverflow.com/questions/33702393/how-to-use-ascii-in-array/33702633#33702633 – Andreas Dec 06 '15 at 21:05
  • I am aware of that but my school tells me I have to so... – SamHat Dec 06 '15 at 21:07
  • btw i got the answer link - http://pastebin.com/S1CWBk8q – SamHat Dec 06 '15 at 21:08
  • Is there any specification of what kind of characters are in the string? Are they all ASCII letters? Can they be any character at all? – Klitos Kyriacou Dec 06 '15 at 21:08
  • @user3437460 My answer link is - http://pastebin.com/S1CWBk8q – SamHat Dec 06 '15 at 21:09
  • Nep they have to be only ascii from 65-90 that is all capital english letters – SamHat Dec 06 '15 at 21:09
  • It's interesting that the question says you can use StringTokenizer, and the answer creates a StringTokenizer instance but makes no use of it whatsoever. – Klitos Kyriacou Dec 06 '15 at 21:12
  • Yes that's a mistake i should probably remove it . – SamHat Dec 06 '15 at 21:13
  • @SamHat Are you doing computer science with IB ? Their syllabus is really weird. – user3437460 Dec 06 '15 at 21:14
  • Nep not doing CS just a tenth grade student in ICSE preparing for his ICSE examination – SamHat Dec 06 '15 at 21:15
  • It's a reasonably good answer (after a little tidying) but there's no need to use ASCII values, just use 'A' and 'Z' which are more readable. – Klitos Kyriacou Dec 06 '15 at 21:18
  • @SamHat What Andreas showed you is the solution I had in mind. You only need a 1D array for this. Finally someone providing a sensible solution and not Maps,Sets or HashMaps for a simple task like this. – user3437460 Dec 06 '15 at 21:18
  • okay thank you for the suggestions – SamHat Dec 06 '15 at 21:20
  • @user3437460 yes, but if there was no restriction on the input character range, you wouldn't be able to reasonably use an array (it would be too big and most of its values would be zero) so a Map would have been better. – Klitos Kyriacou Dec 06 '15 at 21:21
  • @KlitosKyriacou I think you are mistaken. The array is to store the occurrence of each letter. which means the array size is always 26 (or 52 for upper and lower case). For those who overly rely on Java Api and data structures like maps and sets, I wonder how they fare when they move to a new language without all these "facilities". – user3437460 Dec 06 '15 at 21:32
  • Yes I know what you mean, but what I was saying was "if there was no restriction in the input character range" - i.e. input a string that contains any characters from the entire Unicode set. Now try and use an array with that... – Klitos Kyriacou Dec 06 '15 at 21:32

0 Answers0