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). - Public static void main (String[]args){
-
InputStreamReader tr = new InputStreamReader(System.in);
-
BufferedReader br = new BufferedReader (in);
-
String s = new String();
thanks in advance,
carly
5 5031
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). - Public static void main (String[]args){
-
InputStreamReader tr = new InputStreamReader(System.in);
-
BufferedReader br = new BufferedReader (in);
-
String s = new String();
thanks in advance,
carly
You need to take the input as - String s = br.readLine();
Convert the string to array of characters using - char[] chars = s.toCharArray();
Sort the char[] using any sort method you like.
Convert the char[] back to string using
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. - For( (this spot executes at the start) ; (This is condition for continuing the loop) ; (this executes before checking the condition) )
-
//assume input of String s, I dont feel like writing the whole thing
-
String output="";
-
for(int count=0;count<s.length();count++)
-
{
-
output+=s.charAt(s.length()-1-count);
-
//the += function works for string objects, but not other objects
-
}
-
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.
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. - For( (this spot executes at the start) ; (This is condition for continuing the loop) ; (this executes before checking the condition) )
-
//assume input of String s, I dont feel like writing the whole thing
-
String output="";
-
for(int count=0;count<s.length();count++)
-
{
-
output+=s.charAt(s.length()-1-count);
-
//the += function works for string objects, but not other objects
-
}
-
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
{
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |