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 2 of 2

Thread: Strings

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Strings

    I need to write a program using just strings. The problem I am having is figuring out which strings I need to use.

    I have to prompt the user for a sentence that contains 4 words. That's easy, I know how to do that.

    The problem I having is with what you do with the sentence.

    Print the number of letters in each sentence
    I would use str.length();

    Print the average word length
    I have no idea what string to use. I looked through the examples I have and I've never had an example where I've found the average number of words?

    Print each word on a separate line
    Again, I've never been shown an example to know what string to use.

    Print the sentence with the words reversed.
    I've seen several strings that have been used to reverse the sentence, but none that I've ever had an example for and I don't know how to use the ones that I found.

    I just need help with what strings to use. I don't need the program written, I just have no idea what strings should be used to carry out the program.

    Thanks in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Strings

    In a sentence, strings are usually separated by spaces (or whitespaces). To find where one word ends and another starts look through the sentence string looking for white spaces.

    To look a specific character in a string, use the charAt(int loc) method. To extract a word (sub-string) from the sentence, use the substring(int start, int end) method.

    To print out the words in reverse order (without reversing the order of their characters) just print out the last word you found first and the first word you found last.

    To reverse the letters there's a topic in the tips section about reversing strings.

Similar Threads

  1. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM
  2. Replies: 5
    Last Post: January 30th, 2009, 09:31 PM
  3. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM