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

Thread: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    (1) Ask the user to type in a sentence or phrase with between 2 and 5 words (2 really would be fine).

    (2) Display the user’s sentence or phrase.

    (3) Reverse the wording by word.

    (4) Reverse the entire sentence by characters.

    (5) Find a substring in the sentence or phrase and display it. (It need not be an actual word)

    (6) Display only the first word of the user’s sentence or phrase all in upper case letters.

    (7) Display the length of the original sentence or phrase.


    So far..

    System.out.print("Hello user, please enter a sentence or phrase which is two words:");

    String s1, s2;
    s1 = keyboard.next();
    s2 =keyboard.next();
    System.out.println("You entered\"" + s1 + "\" and \"" + s2 + "\"");
    if(s1.equals(s2));

    So I got the program to separate the two strings. I am on step 3 now and I am wondering if i need to equal both the strings in order to continue. I tried up there but i dont think it is right. For example if the user puts : " Hello World" it would be "World Hello." I need help. I know how to reverse the letters of each string but how can i do that to both words. I tried this but it did not work for s2 only s1 which it said olleH:

    String str[]=s1.split("");
    s2.split("");
    String reversedSt="";
    for(int i=str.length-1;i>=0;i--){
    reversedSt+=str[i];


    Please help. I have until wednesday! Thank you!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    I am wondering if i need to equal both the strings
    Can you explain what that means?

    how can i do that to both words.
    Separate the words and reverse each one in turn. Do you know how to write a method? This would be a good place to use one.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    [QUOTE]I am wondering if i need to equal both the strings

    Sorry i did not make it clear. Ok so for step 3 i need to reverse the wording by word. I was wondering how i can do that. I asked the user for two strings. So i am confused on how to do that when i have two strings. You said to separate the words and reverse each one in turn? Can you please explain how. Any explanation would help? thanks

    [/code}
    System.out.print("Hello user, please enter a sentence or phrase which is two words:");
     
    String s1, s2;
    s1 = keyboard.next();
    s2 =keyboard.next();
    System.out.println("You entered\"" + s1 + "\" and \"" + s2 + "\"");
    if(s1.equals(s2));

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    Please post a couple of examples of input and the desired output to avoid any confusion.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

     
    	System.out.print("Hello user, please enter a sentence or phrase which is two words:");
     
    		String s1, s2;
    		s1 = keyboard.next();
    	    s2 =keyboard.next();
     
    		System.out.println("You entered\"" + s1 + "\" and \"" + s2 + "\"");
     
     
    		System.out.println("Reversing the wording by word:" + s2 + " " +  s1 );
    		//step 3
     
    		String source = s1;
     
     
    		for (String part : source.split(" ")) {
    		    System.out.print(new StringBuffer(part).reverse().toString());
    		    System.out.println("");
    		   //step 4

    Hello user, please enter a sentence or phrase which is two words:Hello World
    You entered"Hello" and "World"
    Reversing the wording by word:World Hello
    olleH

    For step 4 I got Hello to reverse its characters but the second string "World" Im not too sure how to do that

    --- Update ---

    System.out.print("Hello user, please enter a sentence or phrase which is two words:");
     
    		String s1, s2;
    		s1 = keyboard.next();
    	    s2 =keyboard.next();
     
    		System.out.println("You entered\"" + s1 + "\" and \"" + s2 + "\"");
     
     
    		System.out.println("Reversing the wording by word:" + s2 + " " +  s1 );
    		//step 3
     
     
     
    		String source = (s1 + s2);
     
    		for (String part : source.split( " ")) {
    		    System.out.print("The reverse of the entire sentece by characters is:" + new StringBuffer(part).reverse().toString());
     
    		    int strlength = s1.length();
     
    		    for(int i=strlength-1;i>=0;i--)
    		    	System.out.println("");
    		    //step 4
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    		  int n = s1.length();
     
    		  //step7
    		  int m = s2.length();
    		  int o= n+m;
    		 System.out.println("The length of your phrase is " + o);
    		    //step 7




    Hello user, please enter a sentence or phrase which is two words:Hello World
    You entered"Hello" and "World"
    Reversing the wording by word:World Hello
    The reverse of the entire sentece by characters is:dlroWolleH




    The length of your phrase is 10


    So far i have this. I was wondering how i could space "dlroWolleH" to be : "dlroW olleH"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    Can you do this:
    Please post a couple of examples of input and the desired output to avoid any confusion.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    What are you talking about?? Havent i been doing that?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    Where are the samples? I was looking for something like this:
    input= "this is the input"
    output = "input the is this"

    A lot of code has been posted. I don't see a simple list of input Strings and output Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    I am new to this. Well it says under the Code box what i put in. It was "Hello World" That was my input and what came out of that was displayed in that box. I dont get you mean sorry.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,112
    Thanks
    65
    Thanked 2,718 Times in 2,668 Posts

    Default Re: String Method(Reverse words, reverse characters,Uppercase, lowercase,length)

    Can you show the String that the program should output if its input is the following String?
    "This is the input"
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Method to take in a string sentence and reverse the tokens
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2012, 05:11 PM
  2. [SOLVED] Method to reverse String using While Loop
    By Montrell79 in forum Loops & Control Statements
    Replies: 2
    Last Post: February 22nd, 2012, 03:49 PM
  3. Uppercase and Lowercase in a string
    By sselasky1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2012, 03:01 PM
  4. Reverse every pair of words in a string
    By mulligan252 in forum Collections and Generics
    Replies: 4
    Last Post: October 11th, 2011, 04:29 PM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM

Tags for this Thread