Codeforces 281A Word Capitalization Solution & Explanation

Difficulty : 800

Problem Description

Capitalization is writing a word with its first letter as a capital letter. Your task is to capitalize the given word.

Note, that during capitalization all the letters except the first one remains unchanged.

Input

A single line contains a non-empty word. This word consists of lowercase and uppercase English letters. The length of the word will not exceed 103.

Output

Output the given word after capitalization.

Examples

InputApPLe
OutputApPLe
Inputkonjac
OutputKonjac

Solution

In this problem, it’s possible to change the first character to uppercase without checking its current case, which could potentially save some processing time.

However, the problem description specifies that the input string won’t be empty, though it might consist of just one character. Therefore, it’s crucial to consider the length of the input string when implementing your solution.

C# Solution

Solution1

string inStr = Console.ReadLine();

if(inStr.Length == 1){
    Console.WriteLine(inStr.ToUpper());
}
else {
    Console.WriteLine(char.ToUpper(inStr[0]) + inStr.Substring(1));
}

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String inStr = scanner.nextLine();
        
        if(inStr.length() == 1){
            System.out.println(inStr.toUpperCase());
        }
        else {
            System.out.println(Character.toUpperCase(inStr.charAt(0)) + inStr.substring(1));
        }
    }
}

Python3 Solution

Solution1

inStr = input()

if(len(inStr)==1):
    print(inStr.upper())
else:
    print(inStr[0].upper()+inStr[1:])

JavaScript Solution

Solution1

var inStr = readline()

if(inStr.length==1){
    print(inStr.toUpperCase());
}
else{
     print(inStr[0].toUpperCase()+inStr.substring(1));
}

Conclusion

In this problem, it tests string splitting and case conversion, which can be considered quite basic.

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 281A – Codeforces

Random Codeforces Posts

Leave a Reply

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