«

»

Nov 24

Printing the First Repeated Character in Given String using Java

This question is asked by Genesis On nov 21, 2010

[code language=”java”]

import java.io.*;

public class FirstRepeat {

public static void main(String[] args) throws IOException {

System.out.println("Enter the String");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
char[] chArray = str.toCharArray();
char repeated = ‘ ‘;
for(int i=0; i {
char compare1 = chArray[i];
for(int j=i+1; j {
char compare2 = chArray[j];

if(compare1 == compare2)
{
repeated = compare1;
break;
}
}

}
System.out.println(repeated);
}

}

[/code]

Output:

Enter the String
Hari kiran
i

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>