Блог пользователя chishi

Автор chishi, 14 лет назад, По-английски
import java.io.*;

public class punctuation
{
    public static void main(String[]args) throws IOException
    {
        BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
        
        System.out.println("Enter a sentence: ");
        String sentence = userInput.readLine();
        
        for (int x=1; x<sentence.length(); x++)
        {
            if (sentence.charAt(x) == ' ')
            {
                if (sentence.charAt(x-1) == ' ')
                {    
                    sentence=removeCharAt(sentence, x-1);
                    x--;
                }
                else if (sentence.charAt(x+1) == ','||sentence.charAt(x) == '.' || sentence.charAt(x) == '!' || sentence.charAt(x) == '?')
                {
                    sentence=exchange(sentence, x-1);
                    x--;
                }
            }
            
            if (sentence.charAt(x) == ',' ||sentence.charAt(x) == '.' || sentence.charAt(x) == '!' || sentence.charAt(x) == '?')
            {
                if ((x+1) == sentence.length())
                {
                    break;
                }
                
                else if (sentence.charAt(x+1) != ' ')
                {
                    sentence=insertSpace(sentence, x+1);
                    x--;
                }
            }
        }
            System.out.println(sentence);
    }
    
    public static String removeCharAt(String s, int pos)
    {
           StringBuffer buf = new StringBuffer(s.length() - 1);
           buf.append(s.substring(0,pos)).append(s.substring(pos+1) );
           return buf.toString();
    }
    
    public static String exchange(String s, int pos)
    {
        StringBuffer sent = new StringBuffer(s.length());
        char temp= s.charAt(pos);
        sent.append(s.substring(0,pos)).append(temp).append(s.substring(pos+2));
        
        return sent.toString();
        
    }
    
    public static String insertSpace(String s, int pos)
    {
        StringBuffer buf = new StringBuffer(s.length() +1);
           buf.append(s.substring(0,pos)).append(' ').append(s.substring(pos) );
           return buf.toString();
    }

}
  • Проголосовать: нравится
  • -21
  • Проголосовать: не нравится

»
14 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
круто