[Algorithm] ๋ฌธ์ž์—ด ToUpper, Split, Join,ToLowerโญ

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,


ToUpper, Split, Join, ToLower



Algorithm

Algorithm








1. Algorithm

์ด์ƒํ•œ๋ฌธ์ž ๋งŒ๋“ค๊ธฐ

์„ค๋ช…
๋ฌธ์ž์—ด s๋Š” ํ•œ ๊ฐœ ์ด์ƒ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋‹จ์–ด์˜ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ๋Œ€๋ฌธ์ž๋กœ, ํ™€์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”.

*์ œํ•œ ์‚ฌํ•ญ
๋ฌธ์ž์—ด ์ „์ฒด์˜ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ, ๋‹จ์–ด(๊ณต๋ฐฑ์„ ๊ธฐ์ค€)๋ณ„๋กœ ์ง/ํ™€์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ํŒ๋‹จํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋กœ ๋ณด์•„ ์ง์ˆ˜๋ฒˆ์งธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

public string solution(string s) {
        string answer = "";
        s= s.ToUpper();
        string[] words = s.Split(' ');
        for(int i =0; i<words.Length;i++)
        {
            char[] c = words[i].ToCharArray();
            for(int j=1; j<words[i].Length; j++)
            {
                if(j%2==1)
                {
                c[j]=char.ToLower(c[j]);
                }
            }
            words[i] = new string(c);
        }
        answer = string.Join(' ',words);
        return answer;
    }


  • ์˜ˆ) s = โ€œmake tilโ€

ToUpper()

  • s = s.ToUpper(); ๋ฌธ์ž์—ด s๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ s์— ๋‹ค์‹œ ์ €์žฅ
  • s = โ€œMAKE TILโ€


Split(โ€˜ โ€˜)

  • string[] words = s.Split(โ€˜ โ€˜); โ€˜ โ€˜ ์ŠคํŽ˜์ด๋ฐ” ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ”
  • words [0] = โ€œMAKEโ€ , words[1] = โ€œTILโ€


Split(string.ToCharArray())

  • char[] c = words[i].ToCharArray();
  • i = 0 ์ผ ๋•Œ c[0] = โ€˜Mโ€™ , c[1] = โ€˜Aโ€™ , c[2] = โ€˜Kโ€™ , c[3] = โ€˜Eโ€™
  • i = 1 ์ผ ๋•Œ c[0] = โ€˜Tโ€™ , c[1] = โ€˜Iโ€™ , c[2] = โ€˜Lโ€™


Split(char.ToLower(c[j]))

  • c[j]=char.ToLower(c[j]);
  • i = 0 ์ผ๋•Œ c -> MaKe


new string(c)

  • words[i] = new string(c) c ๋ฌธ์ž๋ฐฐ์—ด์„ ํ•ฉ์ณ์„œ ๋ฌธ์ž์—ด words[i]์— ์ €์žฅ
  • words[0] = โ€œMaKeโ€ words[1] = โ€œTiLโ€


string.Join(โ€˜ โ€˜ , words)

  • answer = string.Join(โ€˜ โ€˜ , words) answer ์— words[] ๋ฐฐ์—ด์‚ฌ์ด์— โ€˜ โ€˜๋ฅผ ๋„ฃ๊ณ  ํ•ฉ์นœ๋‹ค.
  • answer= โ€œMaKe TiLโ€


์‹œ๊ฐ„ ๊ฑธ๋ฆฐ๋ถ€๋ถ„

  • string, char์— ๊ด€ํ•œ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •
  • ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ๋„ฃ์ง€ ์•Š๊ณ  ์™œ ์•ˆ ๋ฐ”๋€Œ๋Š”์ง€ ์ƒ๊ฐํ–ˆ๋˜ ๋ถ€๋ถ„




[Algorithm] Algorithm


์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ