Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to Iterate through a string?

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default How to Iterate through a string?

    Hi everyone,

    This is my first time posting on this forum, as i've only recently started studying java. I'm still learning the basics and I'm having difficulty trying to solve a certain problem from one of my worksheets.Here is the question.

    Many word-processors can check spelling. One of the corrections applied is to swap 'e' for 'i' if 'i' occurs immediately before 'e' and immediately after 'c'. For example, concieve is corrected to conceive. Write a program that carries out this correction. That is, your program should read a string and print the corrected string (or the original string if no correction needed to be applied.)

    First of all, I understand that these forums are to help, as oppose to giving people the exact answers so thats not why im posting it here. I'm just puzzled as to how to do this. I'm aware il need some sort of for loop and the charAt method to check the characters in the string, but how do I get the program to recognize if an 'i' comes before an 'e' and also how to swap those characters and return the correct spelling?

    Thank you


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How to Iterate through a string?

    Take a look at the api for String, namely the replaceAll() method. You can loop through each rule and use this method to do the replacement
    Last edited by copeg; October 30th, 2010 at 09:02 PM.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to Iterate through a string?

    Quote Originally Posted by copeg View Post
    Take a look at the api for String, namely the replaceAll() method. You can loop through each rule and use this method to do the replacement
    Sorry, but I'm still not sure how to do that. The exercise sheet gave a hint to use the charAt() method to go through each character of the string until it found an 'i' and then to check if the next letter after it was an 'e'. I'm still confused

  4. #4
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: How to Iterate through a string?

    Quote Originally Posted by Stockholm Syndrome View Post
    Sorry, but I'm still not sure how to do that. The exercise sheet gave a hint to use the charAt() method to go through each character of the string until it found an 'i' and then to check if the next letter after it was an 'e'. I'm still confused
    public class FindChars {
     
    	public static void main(String[] args) {
    		String s = "find all ie sequencies or sequences?";
     
    		int i = 0;
    		// loop only till length()-1, since the last char is an 'i'
    		// we will get an StringIndexOutOfBoundsException
    		// note that charAt(i+1) is searched by default
    		while (i < s.length()) {
    			// if the char 'i' is found check if the next char is an 'e'
    			if (s.charAt(i) == 'i' && s.charAt(i+1) == 'e' ) {
    				System.out.println("ie found at position (zero-based) " + i );
    			}
    			i++;
    		}
    	}
    }

  5. The Following User Says Thank You to j2me64 For This Useful Post:

    Stockholm Syndrome (October 31st, 2010)

  6. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to Iterate through a string?

    Thanks, Could you tell me how i can stop the "System.out.println(s.replaceAll("ie", "ie"); from being repeated in the code below? Basically I want it to replace any 'ie' combination in the string with 'ei' and if there is no correction needed, to print out the orignial string.

    String s = "find all ie sequencies or sequences?";
    for(int i=0; i<s.length(); i++)
    		{
    			if(s.charAt(i) == 'i' && s.charAt(i+1) == 'e')
    			{
    				System.out.println("ie found at position (zero-based) " + i );
    				System.out.println(s.replaceAll("ie", "ei"));
    			}
    		}
    Last edited by Stockholm Syndrome; November 2nd, 2010 at 03:37 PM.

  7. #6
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: How to Iterate through a string?

    I am sorry to point out you are missing a step here, if you replaceAll "ie" with "ei", you are not following the outline of the assignment, it is only after a 'c',

    So i would loop through looking for a c, if you find a c, check the next two chars, if they are ei, fine, if they are ie, switch them. if you know the location of the i(followed by an e), you can do
    array[iLoc] = 'e';
    array[iLoc+1] = 'i';

  8. #7
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to Iterate through a string?

    Quote Originally Posted by Zula View Post
    I am sorry to point out you are missing a step here, if you replaceAll "ie" with "ei", you are not following the outline of the assignment, it is only after a 'c',

    So i would loop through looking for a c, if you find a c, check the next two chars, if they are ei, fine, if they are ie, switch them. if you know the location of the i(followed by an e), you can do
    array[iLoc] = 'e';
    array[iLoc+1] = 'i';
    Thanks for your help i completely misunderstood the assignment. But we havnt been introduced to arrays yet at all so i really dont know how to use them & it also means the problem can be solved without them

  9. #8
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: How to Iterate through a string?

    My apologies, so maybe go through making a new string, you have used charAt yes?
    String string = "string to check";
    String outString = "";
    for(int i = 0;i<string.length();i++) {
        outString+= string.charAt(i);
        if(string.charAt(i) == 'c') {
           if(string.charAt(i+1) == 'i' && string.charAt(i+2) == 'e') {
               outString+= "ei";
                i+=2;
            }
        }
    }
    perhaps?

  10. The Following User Says Thank You to Zula For This Useful Post:

    Stockholm Syndrome (November 2nd, 2010)

  11. #9
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: How to Iterate through a string?

    Quote Originally Posted by Zula View Post
    My apologies, so maybe go through making a new string, you have used charAt yes?
    String string = "string to check";
    String outString = "";
    for(int i = 0;i<string.length();i++) {
        outString+= string.charAt(i);
        if(string.charAt(i) == 'c') {
           if(string.charAt(i+1) == 'i' && string.charAt(i+2) == 'e') {
               outString+= "ei";
                i+=2;
            }
        }
    }
    perhaps?
    Yea this is much easier to understand, thanks a lot man

Similar Threads

  1. How can I convert a String to Set<String>? Is it possible?
    By noFear in forum Java Theory & Questions
    Replies: 2
    Last Post: August 25th, 2010, 09:03 AM
  2. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 10:16 AM
  3. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM

Tags for this Thread