aspose file tools
The moose likes Beginning Java and the fly likes Playing with a string - need suggestions. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Playing with a string - need suggestions." Watch "Playing with a string - need suggestions." New topic
Author

Playing with a string - need suggestions.

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I have a string of alphabets only. I want to replace each alphabet by the (n th) alphabet that comes after it in the series of English alphabet.

For example, my string is ABC :

n =1, then ABC-->BCD
n =2, then ABC-->CDE etc...

(If possible, can we try n= -1 so that ABC-->ZAB etc...)

Any help will be highly appreciated.


SCJP 6. Learning more now.
Anooj Narvekar
Greenhorn

Joined: Feb 08, 2011
Posts: 10

You can use ASCII characters to do this.

Where n is the input no. Also you can have an if check to check the rounding.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Welcome to the javaranch Anooj.

Why the "difficult" for loop? Why not just:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Anooj Narvekar
Greenhorn

Joined: Feb 08, 2011
Posts: 10

Hey Wouter,

Ya, the above also works. Well, y the term "difficult" but? The efficiency of code remains same anyways right?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Anooj Narvekar wrote:Well, y the term "difficult" but? The efficiency of code remains same anyways right?

1) UseRealWords: "why", not "y".
2) looping forward looks simpler and is more natural.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

not sure about the efficiency of the code, but the efficiency of someone reading your code goes down. Most coders can glance at Wouter's version and know exactly what it's doing. When I saw your version, I had to study it for a little bit to understand what you were really doing. So any possible efficiency gained is lost a thousandfold in a developers time. And maintenance is usually the highest cost in writing/selling/maintaining an application.


Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
What happens when you get words like "syzygy" and try to add to them?
How about an array like {'a', 'b', 'c' ... 'z'}. You can use the % operator to get the index in the array, add n to that, and use the % operator again to find the replacement character. Use it as a circular array.

Beware. When you work out the correct right operand for %, you find that 'a' or 'A' give you 1, not 0, so you will have to allow for that when looking for the array index.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Sorry, i did not mention this - The string which i am trying to alter is more than three alphabets long. It can have small or capital alphabets ONLY, nothing else is allowed.
I want to retain the case of the alphabet. eg AbCde is BcDef etc. So, i will have to use an array of 26+26 characters (a-z and A-Z) which will consume a "lot of" memory.
Then i will also have to check the case of the alphabet(how do i do that ?) . There is also the tedium of typing 'x' 42 times.
Is there a way to do this without using an array ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Rahul Sudip Bose wrote: . . . an array of 26+26 characters (a-z and A-Z) . . . will consume a "lot of" memory. . . .
Do you really think 104 bytes is a lot of memory? Put six 0s on, and you might have a lot of memory.

You are reminded that a char is not a character. It is a 16-bit unsigned integer, So you can do arithmetic on it. Look at the values of the different letters in Unicode/ASCII. Look at their values. What happens if you use the % operator on a char? Or add a number and subtract 26 if it is greater than 'Z'?
Beware: you will have to cast the result back to a char.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I tried the following approach and it worked. But i will get into trouble if the combination is k =-1 and if string has 'a'/'A' or if k = 1 and string has a z/Z inside it etc. How do i solve this problem ?



I forgot to add : I want Z to become A if the k is 1, Z to become B if k is 2 , A to become Y if k is -2 etc.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Rahul Sudip Bose wrote:I tried the following approach and it worked. But i will get into trouble if the combination is k =-1 and if string has 'a'/'A' or if k = 1 and string has a z/Z inside it etc. How do i solve this problem ?


In other words, "What is the letter after Z" is your problem? That isn't a programming problem, it's a requirements problem. You have to decide what you want it to be. Only then can you program it. So it's up to you.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Campbell Ritchie wrote:
Rahul Sudip Bose wrote: . . . an array of 26+26 characters (a-z and A-Z) . . . will consume a "lot of" memory. . . .
Do you really think 104 bytes is a lot of memory? Put six 0s on, and you might have a lot of memory.


I feel that it is better to save memory whenever possible and when the savings are "significant". I guess that a small saving can become a big one IF : A situation exists in which we have 10^6 such arrays in memory, like maybe when multiple threads using such code are running. (I have just started threads ,please correct me if i am saying something wrong). Is such a situation possible ?
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Paul Clapham wrote:
Rahul Sudip Bose wrote:I tried the following approach and it worked. But i will get into trouble if the combination is k =-1 and if string has 'a'/'A' or if k = 1 and string has a z/Z inside it etc. How do i solve this problem ?


In other words, "What is the letter after Z" is your problem? That isn't a programming problem, it's a requirements problem. You have to decide what you want it to be. Only then can you program it. So it's up to you.


Sorry, i should have added this - I want Z to become A if the k is 1, Z to become B if k is 2 , A to become Y if k is -2 etc.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

this really shouldn't be that hard. The first thing i'd do is note that shifting by X > 25 is the same as shifting by X - 26. And shifting by 0 is trivial. Further, I'd notice that shifting by a negative number X is the same as shifting by 26 - X (and I may be off by one here...).

I have just reduce the number of things I have to worry about. Now I only have to code for shifting 1-25 places, and converting the amount to shift to one of those numbers.

next, I would notice that shifting 'a' by X places is the EXACT same thing as shifting 'A' by X places. In Ascii, shifting from upper to lower case is a simple mathematical operation (addition). So, I can extract out the "case" problem, figure out how to shift the letter, then figure out the correct case.

When I look at this, I can now see several methods I might need:

1) a method to convert the input 'n' to a value from 0-25 instead of any integer.
2) a method that that takes a letter and shifts it a given number (0-25), returning the new letter
3) a method that giving an input letter can pass the upper-case letter to method #2, then figures out if it needs to have its case changed, and returns the appropriate letter
4) a method that reads each character, and passes it in to #3, storing each returned letter in a new string.

That's how I would approach the problem.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
I would disagree about saving memory. (Actually Fred and I have shown that you don't need the array at all). If you are multi-threading, each thread will see the same array. Memory is really cheap nowadays; you can buy a so-called 1TB disc for about £80 (US$130, INR 6000 approx). So only start worrying about memory when you get poor performance. Most JVMs can get their hands on 100MB of RAM easily on a modern PC.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Playing with a string - need suggestions.
 
Similar Threads
regex question
Java array help
Programming Diversion 2b: Applying a hat
creating a list of subsets from N elements
HTTP request 'GET' example