help i cant figure it out.

Write a program that defines and instantiates a new String object called title with COMPUTER PROGRAMMING in it. Then create a new String labeled title2 -- write code to store the contents of title into title1 in reverse order. Using a for loop and charAt.

Recommended Answers

All 5 Replies

Huh? This is JavaScript forum. You should go to this forum instead.

To answer your question, it said that you to create 2 String variables. Assign the value "COMPUTER PROGRAMMING" to the first one, and then iterate through a loop using the first variable length in reverse order. Inside the loop, append each character to the second string using charAt() method. Nothing saying about display the result, but you should be able to by using System.out.print() or System.out.println() method to do so.

Write a program that defines and instantiates a new String object called title with COMPUTER PROGRAMMING in it. Then create a new String labeled title2 -- write code to store the contents of title into title1 in reverse order. Using a for loop and charAt.

You don't need a loop nor a charAt method for this task; The "for in" is sllower + collects colaterals, -you don't need the "charAt" either (also sllow and completely unnecesary). String Objects are also sllow, string literals are faster. So instead of learning the worst approach possible even if being suggested by your school, I propose a professional and efficient coding which will in some degre, satisfy both sides:

var title = "COMPUTER PROGRAMMING",
    title1 = new String( title ), 
    title2 = new String(title.split("").reverse().join(""));

title2 >> "GNIMMARGORP RETUPMOC"

Sorry Troy, the requirement said that the OP needs to use charAt() method to solve the problem. Besides, there is no reverse() or join() method for String class in Java (not JavaScript).

/*
i.e in Java
String title1 = "COMPUTER PROGRAMMING";
String title2 = "";  // if not use StringBuilder class
for (...) {  // do the loop in reverse
  // rebuild the string here one char by one char
}
*/

Thanks for the feedback lol but applepie-0167 is obviously thinking javascript while saying java, and this, of course is a javascrip section, therefore a javascript solutions will be served.
p.s. There's no 'String.reverse' method in javascript either, but there we are. It's the power of Live Script at work! :)

No no Troy, many people who are new to programming often times are confused that Java is JavaScript and vice versa. This requirement is more on programming concept which is DIY. To learn some logics, you may need to learn how a built-in function/method works by implementing it yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.