프로그래머스 | JadenCase 문자열 만들기

Algorithm 2019. 8. 26. 12:12
반응형

JadenCase란 모든 단어의 첫 문자가 대문자이고, 그 외의 알파벳은 소문자인 문자열입니다. 문자열 s가 주어졌을 때, s를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요.제한 조건

  • s는 길이 1 이상인 문자열입니다.
  • s는 알파벳과 공백문자(" ")로 이루어져 있습니다.
  • 첫 문자가 영문이 아닐때에는 이어지는 영문은 소문자로 씁니다. ( 첫번째 입출력 예 참고 )

입출력 예

sreturn

3people unFollowed me 3people Unfollowed Me
for the last week For The Last Week
1
2
3
4
5
6
public class Solution {
    public string solution(string s) {
        string answer = "";
        return answer;
    }
}
 
 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
 
namespace _18
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("JadenCase만들기");
 
            //JadenCase란 모든 단어의 첫 문자가 대문자이고, 그 외의 알파벳은 소문자인 문자열입니다. 
            //문자열 s가 주어졌을 때, s를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요.
            // s는 길이 1 이상인 문자열입니다.
            // s는 알파벳과 공백문자(" ")로 이루어져 있습니다.
            // 첫 문자가 영문이 아닐때에는 이어지는 영문은 소문자로 씁니다. ( 첫번째 입출력 예 참고 )
 
            //case 1 
            var s = "3people unFollowed me";
            //result: "3people Unfollowed Me"
 
            //case 2 
            //var s = "for the last week";
            //result: "For The Last Week"
 
            //case 3
            //var s = " for the last week";
            //Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
            //result: ?
 
            //case 4
            //var s = "for the last  week";
            //Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
            //result: ?
 
            //case 5
            //var s = "for the last week ";
            //result: ?
            //Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
            
 
            var sol = new Solution();
            var result = sol.solution(s);
            Console.WriteLine(result);
        }
    }
 
    public class Solution {
        public string solution(string s) {
            string answer = "";
 
            var arr = s.Split(' ');
            int i = 0;
            foreach(var str in arr){
                var firstStr = str[0].ToString();
                var space = "";
                if(i < arr.Length-1){
                    space = " ";
                }
                if(string.IsNullOrEmpty(firstStr) || firstStr == " " || int.TryParse(firstStr, out int n)){
                    Console.WriteLine("{0} 첫문자가 영문이 아닙니다.", str);
                    answer += str + space;
                }else{
                    var temp = str.Replace(str.Substring(01), str.Substring(01).ToUpper());
                    temp  = temp.Replace(temp.Substring(1), temp.Substring(1).ToLower());
                    answer += temp + space;
                }     
                i++;              
            }
            return answer;
        }
    }
 
}
 
 
 

 

 

 

핵심:

문자열을 문자 배열로 변환 

문자 배열을 문자열로 변환 

문자열 또는 문자를 대문사 소문자로 변환 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
 
namespace _18
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("JadenCase만들기");
 
            //JadenCase란 모든 단어의 첫 문자가 대문자이고, 그 외의 알파벳은 소문자인 문자열입니다. 
            //문자열 s가 주어졌을 때, s를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요.
            // s는 길이 1 이상인 문자열입니다.
            // s는 알파벳과 공백문자(" ")로 이루어져 있습니다.
            // 첫 문자가 영문이 아닐때에는 이어지는 영문은 소문자로 씁니다. ( 첫번째 입출력 예 참고 )
 
            //case 1 
            // var s = "3people unFollowed me";
            //result: "3people Unfollowed Me"
 
            //case 2 
            //var s = "for the last week";
            //result: "For The Last Week"
 
            //case 3
            //var s = " for the last week";
            //result: ?
 
            //case 4
            // var s = "for the last  week";
            //result: ?
 
            //case 5
            // var s = "for the last week ";
            //result: ?
 
            //case 6
            var s = "   for  the    last  week    ";
            //result: ?
            
 
            var sol = new Solution();
            var result = sol.solution2(s);
            Console.WriteLine(result);
        }
    }
 
    public class Solution {
        // public string solution(string s) {
        //     string answer = "";
 
        //     var arr = s.Split(' ');
        //     int i = 0;
        //     foreach(var str in arr){
        //         var firstStr = str[0].ToString();
        //         var space = "";
        //         if(i < arr.Length-1){
        //             space = " ";
        //         }
        //         if(string.IsNullOrEmpty(firstStr) || firstStr == " " || int.TryParse(firstStr, out int n)){
        //             Console.WriteLine("{0} 첫문자가 영문이 아닙니다.", str);
        //             answer += str + space;
        //         }else{
        //             var temp = str.Replace(str.Substring(0, 1), str.Substring(0, 1).ToUpper());
        //             temp  = temp.Replace(temp.Substring(1), temp.Substring(1).ToLower());
        //             answer += temp + space;
        //         }     
        //         i++;              
        //     }
        //     return answer;
        // }
 
        public string solution2(string s){
            
            s = s.ToLower();
            // Console.WriteLine(s);
            var arr = s.Split(' ');
            for(int i = 0; i<arr.Length; i++){
                if(arr[i].Length >= 1){
                    Console.WriteLine("--> " + arr[i]);
                    char[] arrChar = arr[i].ToCharArray();
                    arrChar[0= char.ToUpper(arrChar[0]);
                    arr[i] = new string(arrChar);
                }
            }
 
            var result = arr[0];
            for(int i = 1; i<arr.Length; i++){
                result += " " + arr[i];
            }
 
            return result;
        }
    }
 
}
 
 
 

 

 

https://docs.microsoft.com/ko-kr/dotnet/api/system.string.tochararray?view=netframework-4.8

 

String.ToCharArray Method (System)

이 인스턴스의 문자를 유니코드 문자 배열에 복사합니다.Copies the characters in this instance to a Unicode character array.

docs.microsoft.com

 

https://docs.microsoft.com/ko-kr/dotnet/api/system.char.toupper?view=netframework-4.8

 

Char.ToUpper Method (System)

유니코드 문자를 해당하는 대문자로 변환합니다.Converts the value of a Unicode character to its uppercase equivalent.

docs.microsoft.com

 

반응형
: