Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Playing with a string - need suggestions.  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anooj Narvekar
Greenhorn
Posts: 10
IntelliJ IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the javaranch Anooj.

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

 
Anooj Narvekar
Greenhorn
Posts: 10
IntelliJ IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Wouter,

Ya, the above also works. Well, y the term "difficult" but? The efficiency of code remains same anyways right?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22482
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!