Codeforces-131a-caps LOCK

Source: Internet
Author: User
Tags stringbuffer
A. CAPS LOCK time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard output

WHAT do WE NEED CAPS LOCK for?

Caps Lock is a computer keyboard key. Pressing it sets an input mode into which typed letters are capital by default. If It is pressed by accident, it leads to accidents like the "one" and "we had in the" the "I Passage."

Let's consider that's a word has been typed with the Caps lock key accidentally in switched it only If:either Ercase letters; or all letters except for the one are uppercase.

In this case we should automatically change the case to all letters. For example, the case of the letters that form words "HELLO", "HTTP", "Z" should to be changed.

Write A program This applies the rule mentioned above. If The rule cannot is applied, the program should leave the word unchanged. Input

The ' the ' input data contains a word consisting of uppercase and lowercase Latin letters. The word ' s length is from 1 to characters, inclusive. Output

Print The result of the given word ' s processing. Sample Test (s) input

CAPS
Output
Caps
Input
Lock
Output
Lock

First of all, consider a method, but more complex, mainly in the comparison of whether the strings are all lowercase letters enough, more cumbersome, reflected in two places: 1, the use of circular thinking, to determine whether a single character is lowercase. 2, because the need to use a single character comparison, can only use the StringBuffer class, need to convert. (toLowerCase () function only in string)

Import java.util.*;

public class CapsLock {public
	static void Main (string[] args) {
		Scanner inscanner = new Scanner (system.in);
		String tempstring = Inscanner.nextline ();
		StringBuffer sbuffer = new StringBuffer (tempstring);
		StringBuffer subbuffer = new StringBuffer ("");
		int x = 0;//ini
		boolean isallupper = true;
		Char start = Sbuffer.charat (0);
		if (Sbuffer.length () > 1)
			subbuffer = new StringBuffer (sbuffer.substring (1));
		while (x < Subbuffer.length ()) {
			if (character.islowercase (Subbuffer.charat (x))) {
				isallupper = false;
				break;
			x + +;
		}
		if (isallupper) {
			if (character.islowercase (start))
				System.out.print (Character.touppercase (start));
			else {
				System.out.print (character.tolowercase (start));
			}
			if (Sbuffer.length () > 1) {
				System.out.println (subbuffer.tostring (). toLowerCase ());
			}
		} else
			System.out.println (sbuffer);

	}
Later, after analysis, there are several improvements:

Import Java.util.Scanner;


public class p131a {public
	static void Main (string[] args) {
		Scanner s = new Scanner (system.in);
		String w = s.next ();
		if (w.substring (1). toUpperCase (). Equals (w.substring (1))//Determine whether all uppercase
			System.out.println (
					Character.islowercase (W.charat (0))//The correction of the string output before the
							character.touppercase (W.charat (0)):
							Character.tolowercase (W.charat (0)))
					+ w.substring (1). toLowerCase ());
		Else System.out.println (w);

	}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.