We need to write a program that reads a string (length < 1200) and converts uppercase letters to lowercase, and lowercase letters to uppercase. But careful: The description says: "Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string." That is typical swapping case. However the example: Input: "fAIR, LATER, OCCASIONALLY CLOUDY." Output: "Fair, later, occasionally cloudy." Wait, that's not swapping case; it looks like they have converted the string to "title case"? Actually "fAIR, LATER, OCCASIONALLY CLOUDY." -> "Fair, later, occasionally cloudy." That is: first letter of first word becomes uppercase? But "fAIR" becomes "Fair": The first character 'f' is lowercase, becomes uppercase? Actually "fAIR": characters: f (lowercase), A (uppercase), I (uppercase), R (uppercase). After conversion: "Fair": F (uppercase), a (lowercase), i (lowercase), r (lowercase). That's like converting to only first character uppercase and rest lowercase for each word? But also "LATER" becomes "later": all lowercase. "OCCASIONALLY" becomes "occasionally": all lowercase. "CLOUDY." becomes "cloudy." all lowercase. So overall, the output is: first word capitalized, all other words lowercase. That's not a simple swap. So maybe the description is misleading? Let's re-read: "Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string." That suggests swapping case. But example shows something else.
Check the problem source: Possibly from AOJ (Aizu Online Judge) or similar. Actually I recall a problem: "Case Conversion" sometimes ask to swap case. But there is also "Changing case" problem where you convert uppercase to lowercase and vice versa. But this example is from "ITP1_8_A" maybe? Wait, ITP1_8_A is "Toggle Case" from AIZU. Let me recall: In ITP1_8_A: "Write a program which converts all uppercase letters in a given string to lowercase and all lowercase letters to uppercase." The example: Input: "fAIR, LATER, OCCASIONALLY CLOUDY." Output: "Fair, later, occasionally cloudy." That matches exactly? Actually I recall that ITP1_8_A has example: Input: "fAIR, LATER, OCCASIONALLY CLOUDY." Output: "Fair, later, occasionally cloudy." Yes! That's the actual example for toggling case? Wait, maybe I'm mixing up. Let's check: ITP1_8_A: "Toggle Case". Many solutions exist: read a line, swap case. In many languages, if you just swap case, "fAIR" becomes "Fair"? Let's test: 'f' (lower) -> 'F'; 'A' (upper) -> 'a'; 'I' (upper) -> 'i'; 'R' (upper) -> 'r'. So "fAIR" becomes "Fair". Good. "LATER": 'L'->'l', 'A'->'a', 'T'->'t', 'E'->'e', 'R'->'r' => "later". "OCCASIONALLY": all uppercase, each becomes lowercase => "occasionally". "CLOUDY." -> 'C'->'c', 'L'->'l', 'O'->'o', 'U'->'u', 'D'->'d', 'Y'->'y', '.' remains '.' => "cloudy." So the entire string becomes "Fair, later, occasionally cloudy." Exactly matches example. So it's indeed swapping case: uppercase -> lowercase, lowercase -> uppercase. So the description matches: convert uppercase to lowercase and lowercase to uppercase. The example is consistent with that.
Thus, we need to write a program that reads a line (string) and for each character, if it's uppercase letter, output lowercase; if lowercase, output uppercase; else output as is.
Constraints: length < 1200, which is small.
Implementation: For each character c in string:
- if 'A' <= c <= 'Z': output chr(ord(c) + 32) (since ASCII) but we should use language built-ins if possible. Since we are writing solution generally, we can just check and convert.
- if 'a' <= c <= 'z': output chr(ord(c) - 32)
- else: output c.
We need to read entire line (including spaces). Input ends with newline.
We must print the converted string.
Edge cases: empty line? Possibly not.
We'll write in C++? The user didn't specify language. Since it's typical programming problem, we can write in C, C++, Java, Python, etc. But the instruction: "Write a program which converts..." They likely expect a complete program. I'll provide a solution in Python, as it's simple. But also could provide C++.
Given that we are ChatGPT, we can provide a solution in a common language. Usually for such problems, Python is fine.
Let's produce a Python script:
import sys
def main():
s = sys.stdin.readline()
# note: readline includes newline; we might want to strip newline? But we need to print the converted string without extra newline? Usually we should output exactly the converted line. If we keep newline, conversion may treat newline? No, we process only characters in the line, excluding newline. So we can rstrip? But we want to output exactly one line. So we can process s.rstrip('\n') but then we need to add newline after output. Better: read entire input (maybe there are multiple lines? The problem says "A string is given in a line." So only one line. So we can read line and strip trailing newline, then process, then print. Or we can process and then print with newline automatically.
# Typically, we do:
s = sys.stdin.readline().rstrip('\n')
res = []
for c in s:
if 'A' <= c <= 'Z':
res.append(chr(ord(c) + 32))
elif 'a' <= c <= 'z':
res.append(chr(ord(c) - 32))
else:
res.append(c)
print(''.join(res))
if name == "main":
main()
But note: Python's str.swapcase() does exactly this. But we can also use that. However, typical answer for programming exercises may be to implement manually. But it's okay to use built-in. But the problem might be to teach loops. We'll provide a solution that uses manual conversion.
Alternatively, we can provide a C++ solution:
#include
#include
#include
using namespace std;
int main() {
string s;
getline(cin, s);
for (char &c : s) {
if (isupper(c)) c = tolower(c);
else if (islower(c)) c = toupper(c);
}
cout << s << endl;
return 0;
}
But careful: Using tolower and toupper in C++ from works on int. But if we pass char, it's okay, but need to cast to unsigned char? Actually for safety, but typical.
Which language to choose? The prompt didn't specify. Usually we default to Python.
Also we need to output only one line.
I'll provide a Python solution with explanation.
Since this is a code generation, I'll output the final code in a code block.
But the user asked: "Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string." So they want the code.
I will provide both a Python solution and maybe a C++ solution? But typical ChatGPT answers present one language unless specified.
Given the context, I'll provide Python because it's simple.
But the instructions say: "You are an AI programming assistant..." So I can produce code.
Thus final answer: Provide Python code.
I'll also include comments.
Let's produce:
import sys
def main():
s = sys.stdin.readline().rstrip('\n')
result = []
for ch in s:
if 'A' <= ch <= 'Z':
result.append(chr(ord(ch) + 32))
elif 'a' <= ch <= 'z':
result.append(chr(ord(ch) - 32))
else:
result.append(ch)
print(''.join(result))
if __name__ == "__main__":
main()
Alternatively, we could use str.swapcase():
print(sys.stdin.readline().rstrip('\n').swapcase())
But the manual approach is more educational.
I'll choose the manual approach.
Thus final answer. ```python
import sys
def main():
# Read the entire line (input string) and remove the trailing newline
s = sys.stdin.readline().rstrip('\n')
result = []
for ch in s:
# Check for uppercase letters and convert to lowercase
if 'A' <= ch <= 'Z':
result.append(chr(ord(ch) + 32))
# Check for lowercase letters and convert to uppercase
elif 'a' <= ch <= 'z':
result.append(chr(ord(ch) - 32))
# All other characters remain unchanged
else:
result.append(ch)
# Output the converted string
print(''.join(result))
if name == "main":
main()