write a Java Program to input a line of text and remove the repeated consecutive letters from the text
Answers
Answered by
5
import java.util.Scanner;
public class StringOp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine(); //Taking String Input
byte[] ch = str.getBytes(); //Storing Bytes of String in an array
String newstr = ""; //newstr will contain the newer modified string
newstr = newstr + (char)ch[0];
for(int i=1;i<ch.length;i++)
{
if(ch[i]!=ch[i-1]) //A character from str is added to newstr only if it is not a consecutive one.
{
newstr = newstr + (char)ch[i];
}
}
System.out.println("\nNew String is: "+newstr);
}
}
public class StringOp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine(); //Taking String Input
byte[] ch = str.getBytes(); //Storing Bytes of String in an array
String newstr = ""; //newstr will contain the newer modified string
newstr = newstr + (char)ch[0];
for(int i=1;i<ch.length;i++)
{
if(ch[i]!=ch[i-1]) //A character from str is added to newstr only if it is not a consecutive one.
{
newstr = newstr + (char)ch[i];
}
}
System.out.println("\nNew String is: "+newstr);
}
}
yash1975:
thank you
Similar questions