473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

reading strings..

37
Hi Everyone,

I have been trying to do this question but i dont really know how to start.

- Write a program which will read a string and rewrite it alphabetical order. For example, the word STRING should be written as GINRST.

I did this code, but im having some trouble finishing it off, especially with this part (STRING should be written as GINRST).

Expand|Select|Wrap|Line Numbers
  1. Public static void main (String[]args){
  2. InputStreamReader tr = new InputStreamReader(System.in);
  3. BufferedReader br = new BufferedReader (in);
  4. String s = new String();

thanks in advance,
carly
Nov 2 '06 #1
5 5031
r035198x
13,262 8TB
Hi Everyone,

I have been trying to do this question but i dont really know how to start.

- Write a program which will read a string and rewrite it alphabetical order. For example, the word STRING should be written as GINRST.

I did this code, but im having some trouble finishing it off, especially with this part (STRING should be written as GINRST).

Expand|Select|Wrap|Line Numbers
  1. Public static void main (String[]args){
  2. InputStreamReader tr = new InputStreamReader(System.in);
  3. BufferedReader br = new BufferedReader (in);
  4. String s = new String();

thanks in advance,
carly
You need to take the input as
Expand|Select|Wrap|Line Numbers
  1. String s = br.readLine();
Convert the string to array of characters using

Expand|Select|Wrap|Line Numbers
  1. char[] chars = s.toCharArray();
Sort the char[] using any sort method you like.

Convert the char[] back to string using

Expand|Select|Wrap|Line Numbers
  1. s = new String(chars);
Nov 2 '06 #2
kotoro
32
the method given by the previous answer is probably much more efficient than my answer is going to be, but if this is for a programming class, they probably don't want you to use built in sorts and toCharArray

you could always create a new string and append (and save the resulting new string reference over the old string) the characters as you work backwards in the index of the input string.

I'm assuming you're allowed to use loops.
for loops are while loops with built in spaces for operations to be run at the start of the loop and at the end of each pass.

to interpret my loop if you haven't been taught the use of "for" loops, you can convert it to a while loop.
Expand|Select|Wrap|Line Numbers
  1. For( (this spot executes at the start) ; (This is condition for continuing the loop) ; (this executes before checking the condition) )
Expand|Select|Wrap|Line Numbers
  1. //assume input of String s, I dont feel like writing the whole thing
  2. String output="";
  3. for(int count=0;count<s.length();count++)
  4. {
  5.      output+=s.charAt(s.length()-1-count);
  6. //the += function works for string objects, but not other objects
  7. }
  8.  
This SHOULD work, but I havent bug tested the code I wrote above, you should be able to grasp the concept even if my implementation of it wasn't perfect.

Again, this isn't the most convenient way of doing things, but you might be expected to write your own procedure for this.
Nov 3 '06 #3
r035198x
13,262 8TB
the method given by the previous answer is probably much more efficient than my answer is going to be, but if this is for a programming class, they probably don't want you to use built in sorts and toCharArray

you could always create a new string and append (and save the resulting new string reference over the old string) the characters as you work backwards in the index of the input string.

I'm assuming you're allowed to use loops.
for loops are while loops with built in spaces for operations to be run at the start of the loop and at the end of each pass.

to interpret my loop if you haven't been taught the use of "for" loops, you can convert it to a while loop.
Expand|Select|Wrap|Line Numbers
  1. For( (this spot executes at the start) ; (This is condition for continuing the loop) ; (this executes before checking the condition) )
Expand|Select|Wrap|Line Numbers
  1. //assume input of String s, I dont feel like writing the whole thing
  2. String output="";
  3. for(int count=0;count<s.length();count++)
  4. {
  5.      output+=s.charAt(s.length()-1-count);
  6. //the += function works for string objects, but not other objects
  7. }
  8.  
This SHOULD work, but I havent bug tested the code I wrote above, you should be able to grasp the concept even if my implementation of it wasn't perfect.

Again, this isn't the most convenient way of doing things, but you might be expected to write your own procedure for this.
You function seems to me like it's just reversing the input string. The requirement was to sort the characters alphabetically.
Nov 3 '06 #4
kotoro
32
I Must apologize, I only realized my error after the Edit period passed, my code reverses the string's character order.
I didn't read closely enough, and Assumed. (insert cliche about assuming here)

If you need to sort Characters the previous method is the way to go. You might be expected to write the sort algorithm yourself though. If you need to sort words, then you might want to write a loop that checks for spaces and cuts out words piece by piece.

P.S. I feel like an ass now. I need to read more carefully before trying to help next time.
Nov 3 '06 #5
r035198x
13,262 8TB
I Must apologize, I only realized my error after the Edit period passed, my code reverses the string's character order.
I didn't read closely enough, and Assumed. (insert cliche about assuming here)

If you need to sort Characters the previous method is the way to go. You might be expected to write the sort algorithm yourself though. If you need to sort words, then you might want to write a loop that checks for spaces and cuts out words piece by piece.

P.S. I feel like an ass now. I need to read more carefully before trying to help next time.
Naturally I assumed the OP would write the sort function him/herself. After getting the logic it is the least I expected the OP to do.
Yeah we've all overlooked something in a post before. The important thing is to be able to realise it (like you've done) and point it out for the help of others.
Nov 3 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
24
by: Hendrik Schober | last post by:
Hi, I have a 'std::istream' and need to read its whole contents into a string. How can I do this? TIA; Schobi
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
0
by: rth | last post by:
I need to read the Security Event Log and then show the EVENTLOGRECORD as a String. Reading records works fine but when I format the string that I'm going to show the FormatMessage function...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
9
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: otorojava | last post by:
The script works but I'm having a problem reading double and boolean import java.io.*; import java.util.StringTokenizer; import javax.swing.JOptionPane; public class FileAddress {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.