Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Incrementing every letter in a string that occupies an odd position.

807580Mar 21 2010 — edited Mar 22 2010
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)
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2010
Added on Mar 21 2010
10 comments
593 views