Adrian DeRose
Adrian DeRose

Reputation: 91

Java Loops -- Strings of First and last characters of every word

I am currently in an Intro to Programming class at San Jose and as part of our assignment we were to create a class with methods that either return a string with the first letters of each word or the last letters of each word.

The instance variable "phrase" holds the phrase that is accessed in the methods.

Here are the rules:

The words are separated by spaces, It starts with a letter, It does not end with a space, There are never 2 consecutive spaces, There are never 2 consecutive digits or punctuation.

Both the firstLetter() and lastLetter() methods must return an empty string if the phrase is empty.

My question is: What is a more efficient solution to this problem? I am new to algorithms so I would appreciate a more seasoned approach to this simple problem. In the firstLetter() and the lastLetter() method, would I check the status of two characters at a time within the for loop or just one?

Here is my code:

/**
 * Processes first and last letters of words
 * @author (Adrian DeRose)
 */
public class StartToFinish
{
    private String phrase;

    /**
     * Constructs a StartToFinish object
     * @param myString the phase for this object
     */
     public StartToFinish(String myString)
     {
         this.phrase = myString;
     }

    /**
     * Gets first letter of every word in string. 
     * 
     * @return first letter of every word in string
     */
    public String firstLetters()
    {   
        String firstLetters = "";

        if (Character.isLetter(this.phrase.charAt(0)))
        {
            firstLetters += this.phrase.substring(0,1);
        }

        for (int i = 1; i < this.phrase.length(); i++)
        {
            char currentCharacter = this.phrase.charAt(i);
            String previousCharacter = Character.toString(this.phrase.charAt(i-1));
            if (Character.isLetter(currentCharacter) && previousCharacter.equals(" "))
            {
                String characterString = Character.toString(currentCharacter);
                firstLetters += characterString;
            }
        }
        return firstLetters;

    }

    /**
     * Gets last letter of every word in string. 
     * 
     * @return last letter of every word in string
     */
    public String lastLetters()
    {
        String lastLetters = "";
        char lastCharacter = this.phrase.charAt(lastIndex);

        if (this.phrase.length() == 0)
        {
            return "";
        }

        for (int i = 1; i < this.phrase.length(); i++)
        {
            char currentCharacter = this.phrase.charAt(i);
            char previousCharacter = this.phrase.charAt(i-1);

            if (Character.isLetter(previousCharacter) && !Character.isLetter(currentCharacter))
            {
                String previousCharacterString = Character.toString(previousCharacter);
                lastLetters += previousCharacterString;
            }
        } 

        if (Character.isLetter(lastCharacter))
        {
            lastLetters += Character.toString(lastCharacter);
        }

        return lastLetters;
    }
}

Thank you!

Upvotes: 0

Views: 2795

Answers (4)

Kaushik
Kaushik

Reputation: 3381

If I understand your question correctly, I think this is what you're looking for:-

public class StartToFinish {

    private String phrase;
    private String[] words;

    private String firstLetters = "";
    private String lastLetters = "";

    /**
     * Constructs a StartToFinish object
     * 
     * @param myString
     *            the phase for this object
     */
    public StartToFinish(String myString) {
        this.phrase = myString;
        words = phrase.split(" ");

        for (String string : words) {
            if (string.length() == 0)
                continue;

            if (Character.isLetter(string.charAt(0))) {
                firstLetters += string.charAt(0);
            }

            if (Character.isLetter(string.charAt(string.length() - 1))) {
                lastLetters += string.charAt(string.length() - 1);
            }
        }
    }

    /**
     * Gets first letter of every word in string.
     * 
     * @return first letter of every word in string
     */
    public String firstLetters() {
        return firstLetters;
    }

    /**
     * Gets last letter of every word in string.
     * 
     * @return last letter of every word in string
     */
    public String lastLetters() {
        return lastLetters;
    }
}

Upvotes: 0

Aks
Aks

Reputation: 395

One of the solution I will provide is : 1. Check if the phrase is empty in the constructor. 2. Begin with a split, and then do some check.

In the constructor (This isn't needed in your case btw)

splitedPhrase = phrase.split(' ');

In the dedicated function

public String firstLetters() {
String result = "";
for(String word : splitedPhrase) {
    if (Character.isLetter(word.charAt(0)))
        result+=word.charAt(0);
}
return result; 
}

And you just have to change the charAt for the LastLetter function, like word.charAt(word.length-1)

Hope this help, despite some people already posted, I think this will better do what your algortihm need.

Upvotes: 0

Jay Prakash
Jay Prakash

Reputation: 805

I don't think so you have to write all those code just use java function:

        String a = "Hello";
        System.out.println("First:"+a.charAt(0));
        System.out.println("Last:"+a.charAt(a.length()-1));

Output:

First:H
Last:o

Upvotes: 1

Sonic
Sonic

Reputation: 34

i don't know if this is what you are looking for, but this is much more simple way to write the same (sorry for my English)

String a="john snow winter is comming";
    String[] parts = a.split(" ");
    for(String word:parts){
        System.out.println("first letter "+word.charAt(0)+ " last letter "+word.charAt(word.length()-1));
    }

Upvotes: 1

Related Questions