Incrementing every letter in a string that occupies an odd position.
Hi, for an "encryption" project, i need your help creating a method that will be incrementing
by 5 every char in a string that occupies an odd position in that same string.
For example : 'adam' should return 'aiar'
-Method can't use arrays.
-Method can't use regular expressions like 'split' etc..in order words the teacher wants us
to use the simplest stuff inside the String class.
-Only letters must be modified.
so far i have this : (names of values are in french, sorry)
Code :
public class Main {
public static String incrementerDeCinqImpaires (String chaine) {
char j = chaine.charAt(1);
char x = chaine.charAt(3);
char o = chaine.charAt(5);
j+=5;
x+=5;
o+=5;
System.out.println(x);
System.out.println(j);
System.out.println(o);
String tmpString = chaine.replace( chaine.charAt(1), j );
String tmpString2 = tmpString.replace( tmpString.charAt(3), x );
String tmpString3 = tmpString2.replace( tmpString2.charAt(5), o );
System.out.println( "Original = " + chaine );
System.out.println( "Result = " + tmpString3 );
return tmpString3;
}
public static void main (String args [] ) {
System.out.println (incrementerDeCinqImpaires("adam"));
}
}
The thing is i need a "for" loop (which has been a nightmare for me trying to figure out how
to make it) in order to increment every odd positioned char in the string.
So all I need really is to sum up what i posted above inside a for loop (or more). Then just
add ifs and elses so i can increment only letters.
Re: Incrementing every letter in a string that occupies an odd position.
Re: Incrementing every letter in a string that occupies an odd position.
Darryl I don't think there's anything wrong with posting to seperate forums. I understand why there might be one with two sperate groups of people working towards the same answer but I'm not sure.
Anyways to the question what you need is indeed a for loop and that for loop will need to run from the first letter in the string to the last letter and if it is odd it will need to do the increment
Code :
For(....)
{
If(....)
{
.....(you could maybe have a method to handle this part)
}
}
What I want you to do is try to write what I left out because I want you to figure this out for yourself in the main. So have a stab at that or at least write your thoughts of how it should work. Also if this has already been solved in the other forums change the title to show that.
You'll want to take a look at this
String (Java 2 Platform SE 5.0)
Also to replace individual characters you may have to use a StringBuilder instead I'm not sure, so check this out also
StringBuilder (Java 2 Platform SE 5.0)
Re: Incrementing every letter in a string that occupies an odd position.
Quote:
Originally Posted by
Faz
Darryl I don't think there's anything wrong with posting to seperate forums.
And where did I say it's wrong, that you felt the need to point that out to me?
db
Re: Incrementing every letter in a string that occupies an odd position.
Just the impression I got. My apologies so.