Question
Input a sentence from a user that ends with a full stop and remove all the extra spaces from the sentence.
Enter the Sentence:
The quick brown fox jumped over the lazy dog.
Sentence after removing extra Spaces:
The quick brown fox jumped over the lazy dog.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class RemoveExtraSpaces
{
public static void main()
{
int i=0,len=0;
String sen="",newSen="",wd="";
char ch=' ',lastCharacter=' ';
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence:");
sen=sc.nextLine();
len=sen.length();
lastCharacter=sen.charAt(len-1);
if(lastCharacter!='.')
{
sen=sen+".";
len++;
}
for(i=0;i< len;i++)
{
ch=sen.charAt(i);
if(ch==' '||ch=='.')
{
if(wd.equals("")==false)
{
newSen=newSen+wd;
newSen=newSen+ch;
}
wd="";
}
else
{
wd=wd+ch;
}
}
System.out.println("Sentence after removing extra Spaces:");
System.out.println(newSen);
}
}
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
