Write a program to find first non repeating character in a string


This Java code prints out the non-repeated characters in a given string. It works by iterating through the string character by character and comparing each character to all other characters in the string. If a character is found to be repeated, it is skipped. Otherwise, it is printed as a non-repeated character. Here is a breakdown of how the code works:

  • Define a string variable str and initialize it to the value "Java".
  • Print out the given string using System.out.println("Given String : " + str); .
  • Iterate through each character in the string using a for loop with a loop variable i.
  • Set a boolean variable u to true, which will be used to determine whether or not a character is repeated.
  • Iterate through all other characters in the string using another for loop with a loop variable j.
  • If i and j are not equal (i.e., we are not comparing the same character to itself) and the characters at positions i and j are the same, set u to false and break out of the inner loop.
  • If u is still true after comparing the current character to all other characters in the string, print out the current character using System.out.println(str.charAt(i)); .
  • Repeat steps 4-7 for each character in the string.

Source Code

class Non_Repeating
{
	public static void main(String[] args)
	{
		String str = "Java";
		System.out.println("Given String : " + str);
		System.out.println("Non Repeated Character in String is :" );
		for (int i = 0; i < str.length(); i++)
		{
			boolean u = true;
			for (int j = 0; j < str.length(); j++)
			{
				if (i != j && str.charAt(i) == str.charAt(j))
				{
					u = false;
					break;
				}
			}
			if (u)
			{
				System.out.println(str.charAt(i));
			}
		}
	}
}
 

Output

Given String : Java
Non Repeated Character in String is :
J
v

Example Programs