PROGRAM 17 : ENTER A STRING AND REMOVE REPEATED CHARACTERS FROM STRING.
Example:
PROGRAM :
import java.util.Scanner;
class RemoveRepChar
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word:");
String s = sc.nextLine();
s = s + " "; // Adding a space at the end of the word
int l=s.length(); // Finding the length of the word
String ans=""; // Variable to store the final result
char ch1,ch2;
for(int i=0; i<l-1; i++)
{
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1!=ch2) // Check that consecutive characters are not equal
ans = ans + ch1;
}
System.out.println("Word after removing repeated characters = "+ans); // Printing the result
}
}
Example:
PROGRAM :
import java.util.Scanner;
class RemoveRepChar
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word:");
String s = sc.nextLine();
s = s + " "; // Adding a space at the end of the word
int l=s.length(); // Finding the length of the word
String ans=""; // Variable to store the final result
char ch1,ch2;
for(int i=0; i<l-1; i++)
{
ch1=s.charAt(i);
ch2=s.charAt(i+1);
if(ch1!=ch2) // Check that consecutive characters are not equal
ans = ans + ch1;
}
System.out.println("Word after removing repeated characters = "+ans); // Printing the result
}
}
No comments:
Post a Comment