본문 바로가기

   
Programming/C#

indexOf 문제

반응형

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleEx

{

    class array7

    {

        public static void Main(string[] args)

        {

            string data = "가나다라가나다라가나다라가나다라";

 

            int index = -1;

            int startIndex = 0;

 

            while (true)

            {

                index = data.IndexOf("닷넷", startIndex);

 

                if (index > -1)

                {

                    Console.WriteLine(index);

                    startIndex = index + 2;

                }

                else

                    break;

            }

        }

    }

}

 


 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleEx

{

    class quiz2_20_2

    {

        public static void Main(string[] args)

        {

            Console.WriteLine(", , 가운데에 공백이 들어간 문자열을 입력하세요.");

            Console.Write("입력 : ");

            string input = Console.ReadLine();

            int i, j, k = 0;

 

            for (i = 0; ;)

                if (input.IndexOf(" ", i) == i) i++;

                else break;

 

            for (j = input.Length - 1; ; j--)

                if (input.LastIndexOf(" ", j) == j) k++;

                else break;

 

            Console.WriteLine("입력한 문자열 : [{0}]", input);

            input = input.Replace(" ", "");

            Console.Write("가운데 공백 제거 : [");

 

            for (; i > 0; i--)

                Console.Write(" ");

            Console.Write("{0}", input);

            for (; k > 0; k--)

                Console.Write(" ");

 

            Console.WriteLine("]");

        }

    }

}


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace String

{

    class string_03

    {

        public static void Main(string[] args)

        {

            string jumin = "841219-2456547";

 

            jumin = jumin.Replace("-", "");

 

            Console.WriteLine(jumin);

 

            int sum = 0;

 

            for (int i = 0; i < 12; i++)

            {

                //Console.WriteLine("{0} X {1}", jumin.Substring(i, 1), i);

                //Console.WriteLine("{0} X {1}, {2}", jumin[i].ToString(), (i % 8) + 2, i);

                sum += int.Parse(jumin.Substring(i, 1)) * ((i % 8) + 2);

            }

 

            int temp = sum % 11;

            temp = 11 - temp;

            temp = temp % 10;

 

            if (temp.ToString() == jumin.Substring(12, 1))

                Console.WriteLine("올바른..");

            else

                Console.WriteLine("올바르지 않은");

 

        }

    }

}



using System;

 

namespace ConsoleEx

{

    class quiz2_20_3

    {

        public static void Main(string[] args)

        {

            string input;

            Console.WriteLine("주민등록번호 유효성 검사하기");

            int jmc = 1;

            while (jmc == 1)

            {

                Console.Write("주민등록번호 입력 : ");

                input = Console.ReadLine();

                string yoon = "평년";

                input = input.Replace("-", "");

                while (true)

                    if (input.Length == 13) break;

                    else

                    {

                        Console.Write("재입력 : ");

                        input = Console.ReadLine();

                        input = input.Replace("-", "");

                    }

 

                int year = 0, month = 0, day = 0, maxDay = 0;

 

                switch (int.Parse(input.Substring(6, 1)))

                {

                    case 1:

                    case 2:

                    case 5:

                    case 6: year += 1900; break;

                    case 3:

                    case 4:

                    case 7:

                    case 8: year += 2000; break;

                    default: year += 1800; break;

                }

                year += int.Parse(input.Substring(0, 1)) * 10 + int.Parse(input.Substring(1, 1));

 

                month += int.Parse(input.Substring(2, 1)) * 10 + int.Parse(input.Substring(3, 1));

 

                day += int.Parse(input.Substring(4, 1)) * 10 + int.Parse(input.Substring(5, 1));

 

 

                if ((year % 4 == 0 && year % 100 == 0 && year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) yoon = "윤년";

 

                switch (month)

                {

                    case 2:

                        if (yoon == "윤년") maxDay = 29;

                        else maxDay = 28; break;

                    case 1:

                    case 3:

                    case 5:

                    case 7:

                    case 8:

                    case 10:

                    case 12: maxDay = 31; break;

                    default: maxDay = 30; break;

                }

 

                if ((month > 0 && month <= 12) && (day > 0 && day <= maxDay)) jmc = Check(input);

                if (jmc == 1) Console.WriteLine("올바르지 않는 주민등록번호 입니다.");

            }

            Console.WriteLine("올바른 주민등록번호 입니다.");

        }

 

        public static int Check(string jm)

        {

            int i = 0, sum = 0, j = 2;

            for (; i <= jm.Length - 2; i++)

            {

                sum += int.Parse(jm.Substring(i, 1)) * j++;

                if (j == 10) j = 2;

            }

            if (int.Parse(jm.Substring(12, 1)) == 11 - sum % 11) return 2;

            else return 1;

        }

    }

}


 


반응형