• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

code for splitting string into characters

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can any one provide me the code for converting string to characters without using built in methods

example String s="javaranch";
char c[] must hold individual char's for a string s.

please somebody provide me the code

thanks in advance
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serialize the String using StringWriter and read it in using a character-based reader.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not supposed to use predefined methods

if u want more details u just extract src.zip file and then open String class there u can find compareTo() method it uses something different code for splitting the string into characters

can any one see that code and tell me how it's been implemented???

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you just want to loop over the string and copy it's characters into a new array of char. You'll need to create the char first using the length of the string. All of that is simple enough to code out. Unless you are trying to optimize for memory consumption or someting else crazy I don't see where you're having trouble.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problem is i should not use predefined methods for extracting the characters from the string
 
Bartender
Posts: 9559
12
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is obviously a homework assignment or this discussion belongs in Programming Diversions. Noone would jump through these kinds of hoops except for some sort of academic exercise.
The implementation of compareTo compares the char[] value which is internal to the String class. value is private to the String class so you cannot access it without invoking a method (i.e. charAt() or toCharArray()).
There's obviously something more to your assignment, because the task you've stated (String to char[] without invoking methods) cannot be done.
[ July 11, 2006: Message edited by: Joe Ess ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!