Course Solutions Uncategorized (Answered) : Write a fragment that prints the 3rd letter in the string variable called “message”

(Answered) : Write a fragment that prints the 3rd letter in the string variable called “message”

Write a fragment that prints the 3rd letter in the string variable called “message”

Expert Answer


Hi

I assume that you need code fragment in any language.

I have used Java langugae.

CODE

public class MyClass {
public static void main(String args[]) {

//string message
String str = “message”;

//loop to iterate over string
for (int i = 1, n = str.length(); i < n; i++) { //if iteration is 3rd char value i.e. i=3 if(i==3) { System.out.println( str.charAt(i)); return ; //break loop } } } } PFA https://d2vlcm61l7u1fs.cloudfront.net/media%2Ff9d%2Ff9d04402-0b34-47ee-9a55-7507c69b833f%2Fphpq4qqfg.png

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post