Array-promo

Question

Remove all punctuation marks or special characters and then reverse the sentence(word-wise) in a given sentence. 

				
					
Enter the sentence with punctuation marks:
quick> brown? fox< jumped\ over? the: "lazy/ dog.
dog lazy the over jumped fox brown quick


Enter the sentence with punctuation marks:
I went to/*-+'][;\/., meet.-*/ "sandy"
sandy meet to went I
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class reverseWordsinsentence
{
    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 with punctuation marks:");
        sen=sc.nextLine();
        len=sen.length();
        lastCharacter=sen.charAt(len-1);
        if(Character.isLetter(lastCharacter)==true||Character.isDigit(lastCharacter)==true)
        {
            sen=sen+".";  
            len++;
        }
        for(i=0;i< len;i++)
        {
            ch=sen.charAt(i);
            if(ch==' '||i==len-1)
            {
                if(i==len-1)
                {
                    newSen=wd+newSen;
                }
                else
                {
                    newSen=wd+newSen;
                    newSen=" "+newSen; 
                }

                wd="";

            }
            else
            {
                if(Character.isLetter(ch)==true||Character.isDigit(ch)==true)
                {
                    wd=wd+ch;  
                }
            }

        }
        System.out.println(newSen);
    }
}


				
			

Coding Store

Leave a Reply

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