Write a program to check if the letter is present in the word


The code is checking if the given string "String Exercises" contains the letter 'x' or not. It initializes a boolean variable pre to false. It then iterates over each character of the string using a for loop and checks if the character is equal to 'x'. If it finds 'x' in the string, it sets pre to true and breaks out of the loop.

Finally, it prints the value of pre which would be true if the letter 'x' is present in the given string, otherwise false.

Source Code

class Check_Letter
{
	public static void main(String[] args)
	{
		String str = "String Exercises";
		boolean pre = false;
		for(int i = 0;i<str.length();i++)
		{
			if(str.charAt(i) == 'x')
			{
				pre=true;
				break;
			}
		}
		System.out.println(pre);
	}
}

Output

true

Example Programs