By GokiSoft.com| 10:08 05/10/2021|
C Sharp

Tính tổng số chắc & Tổng số lẻ trong chuỗi - Lập Trình C# - Array Trong C#

Nhập vào từ bàn phím chuỗi str.

Yêu cầu thực hiện như sau. Tao mảng số nguyên chứa tất cả các mã của từng ký tự trong chuỗi str

Ví dụ: str = Xin chao

- Lấy mã code của ký tự X, i, n, dấu cách, .... => Lưu vào mảng

- Tính tổng các số chẵn & số lẻ

- Sắp xếp theo thứ tự tăng dần của mảng số nguyên trên.

- Tạo chuỗi từ mảng số nguyên vừa được sắp xếp => Hiển thị kết quả ra màn hình

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-06-25 12:25:22



using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;

namespace Lession1
{
    class Class4
    {
        static void Main(string[] args) {
            Console.WriteLine("Nhap vao mot chuoi : ");
            string SrcString = Console.ReadLine();
            int StringLength = SrcString.Length;
            int chan = 0;
            int le = 0;
            int[] mang = new int [StringLength]; 
            for (int i = 0; i < StringLength; i++) {
                int code = (byte)(SrcString[i]);
                mang[i] = (code);
                if (code % 2 == 0)
                {
                    chan += code;
                }
                else {
                    le += code;
                }
            }
           
            for (int i = 0; i < mang.Length - 1; i++) {
                for (int j = i + 1; j < mang.Length -1 ; j++) {
                    if (mang[i] > mang[j]) {
                        int temp = mang[i];
                        mang[i] = mang[j];
                        mang[j] = temp;
                    }
                }
            }
            Console.WriteLine("Tong so chan {0}, tong so le {1}", chan, le);
            for (int i = 0; i < mang.Length; i++) {
                Console.WriteLine(mang[i]);
            }
        }
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-06-24 14:07:18



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bai6
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Xin chao";
            int[] arr = new int[8];
            for (int i = 0; i < str.Length; i++)
            {
                int a = str[i];
                arr[i] = a;
                Console.WriteLine(a);
            }
            int chan = 0, le = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] % 2 == 0)
                {
                    chan += arr[i];
                }
                else
                {
                    le += arr[i];
                }
            }

            Console.WriteLine();
            Console.WriteLine("Chan: " + chan);
            Console.WriteLine("Le: " + le);
            Console.WriteLine();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[i] > arr[j])
                    {
                        int tmp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tmp;
                    }
                }
            }
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            for (int i = 0; i < arr.Length; i++)
            {
                char str2 = Convert.ToChar(arr[i]);
                Console.WriteLine(str2);

            }
            Console.ReadLine();
        }
    }
}



Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-05-20 13:47:40



using System;

namespace kytu
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.Write("\nEnter String : ");
            string str = Console.ReadLine();
            int[] s = new int[str.Length];

            convertStr_Int(s,str);
            sum(s);
            sort(s);
            convertInt_Str(s);

            
            
        }
        static void sort(int[] s)
        {
            Console.WriteLine("\n sap xep : ");
            int N = s.Length;
            for (int i = 0; i < N - 1; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    if (s[i] > s[j])
                    {
                        int temp = s[i];
                        s[i] = s[j];
                        s[j] = temp;
                    }
                }

            }
            foreach (int n in s)
            {
                Console.Write("{0} ,",n);
            }
        }
        static void convertStr_Int(int[] s,string str)
        {
            int k = 0;
            foreach (char c in str)
            {
                s[k] = (int)c;
                Console.Write("{0} ,",s[k]);
                k++;
            }
        }
        static void convertInt_Str(int[] s)
        {
            Console.Write("\nnew string :  ");
           int k = 0;
            foreach (int n in s)
            {
                Console.Write((char)s[k]);
                k++;
            }
        }
        static void sum(int[] s)
        {
            int tongchan = 0, tongle = 0;
            foreach(int n in s)
            {
                if(n % 2 == 0)
                {
                    tongchan += n;
                }
                else
                {
                    tongle += n;
                }
                
            }
            Console.WriteLine("\nTong so chan : {0} \n Tong so le : {1}",tongchan,tongle);
        }
    }
}



Luong Dinh Dai [T1907A]
Luong Dinh Dai

2020-05-20 05:36:46



using System;

namespace Baitap
{
    class Program
    {
        static void Main(string[] args)
        {
           

           


            Console.WriteLine("Nhap 1 string bat ki");
            string data = Console.ReadLine();
            Console.WriteLine("data = {0}", data);
            char[] charArr = data.ToCharArray();
            int index = charArr.Length;
            int[] intArr = new int[index];
            Console.WriteLine("In mang char");
            for (int i = 0; i < charArr.Length; i++)
            {
                Console.WriteLine("charArr[{0}] ={1}", i, charArr[i]);

                //Console.WriteLine(" ASCII : " + Convert.ToInt32(charArr[i]));

                intArr[i] = Convert.ToInt32(charArr[i]);
            }

            sortASC(intArr);
            Console.WriteLine("Mang sap xep theo thu tu tang dan ");
            int sumChan = 0;
            int sumLe = 0;
            char[] array3 = new char[index];
            for (int i = 0; i < intArr.Length; i++)
            {
                Console.WriteLine("intArr[{0}] ={1}", i, intArr[i]);
                array3[i] = Convert.ToChar(intArr[i]);
                if (intArr[i] % 2 == 0)
                {
                    sumChan += intArr[i];
                }
                else
                {
                    sumLe += intArr[i];
                }


            }
           
            Console.WriteLine("Tong cac so chan trong mang ={0}", sumChan);
            Console.WriteLine("Tong cac so le trong mang ={0}", sumLe);
            string newString = new string(array3);
            Console.WriteLine("new String ={0}", newString);








            #endregion
        }
        public static void sortASC(int[] arr)
        {

            for (int i = 0; i < arr.Length - 1; i++)
            {
                for (int j = i + 1; j < arr.Length; j++)
                {

                    if ((arr[i] % 2 != 0 && arr[j] % 2 == 0)
                            || (arr[i] % 2 == 0 && arr[j] % 2 == 0 && arr[i] > arr[j])
                            || (arr[i] % 2 != 0 && arr[j] % 2 != 0 && arr[i] > arr[j]))
                    {
                        int index = arr[i];
                        arr[i] = arr[j];
                        arr[j] = index;
                    }

                }
            }
        }

    }
}






Đường Thanh Bình [T1907A]
Đường Thanh Bình

2020-05-19 14:04:06



using System;
using System.Text;

namespace Tinhtongsochanle
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Nhập một chuỗi bất kì:");
            String str = Console.ReadLine();
            char[] mang = str.ToCharArray();
            int[] number = new int[mang.Length];

            for (int i = 0; i < mang.Length; i++)
            {
                number[i] = (int)mang[1];
            }

            TinhTong(number);
            SapXep(mang, number);
        }
        public static void TinhTong(int[] number)
        {
            int tongChan = 0, tongLe = 0;
            for (int i = 0; i < number.Length; i++)
            {
                if (number[i] % 2 == 0)
                {
                    tongChan += number[i];
                }
                else
                {
                    tongLe += number[i];
                }
            }
            Console.Write("Tổng số chẵn : {0}", tongChan);
            Console.WriteLine();
            Console.Write("Tổng số lẻ : {0}", tongLe);
            Console.WriteLine();
        }

        public static void SapXep(char[] mang, int[] number)
        {
            int a;
            for (int i = 0; i < number.Length - 1; i++)
            {
                for (int j = i + 1; j < number.Length; j++)
                {
                    if (number[i] > number[j])
                    {
                        a = number[j];
                        number[j] = number[i];
                        number[i] = a;
                    }
                }
            }

            Console.Write("Mnagr khi sắp xếp lại : ");
            for (int i = 0; i < number.Length; i++)
            {
                mang[i] = (char)number[i];
                Console.Write(mang[i]);
            }
        }

    }
}



hoangduyminh [T1907A]
hoangduyminh

2020-05-19 04:54:28



using System;

namespace Lession2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            Console.WriteLine("Nhap vao Chuoi:");
            str = Console.ReadLine();
            int[] a = new int[str.Length];
            char[] n = str.ToCharArray();

            Console.Write("Ma cua cac ki tu vua nhap vao la: ");
            for (int i = 0; i < str.Length; i++)
            {
                a[i] = Convert.ToInt32(n[i]);
                Console.Write("{0} ", a[i]);
            }
            sapxep(a, n);
            tinhtong(a);
        }
        static void sapxep(int[] a, char[] n)
        {
            int N = a.Length;
            for (int i = 0; i < N - 1; i++)
            {
                for(int j = i+1; j < N; j++)
                {
                    if( a[i] > a[j])
                    {
                        int temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            Console.WriteLine("\nMang sau khi sap xep:");
            for( int  i = 0; i < a.Length; i++)
            {
                n[i] = (char) a[i];
                Console.Write(n[i]);
            }
        }
        static void tinhtong(int[] a)
        {
            int sumChan = 0;
            int sumLe = 0;
            for(int i = 0; i< a.Length; i++)
            {
                if(a[i] % 2 == 0)
                {
                    sumChan += a[i];
                }
                else
                {
                    sumLe += a[i];
                }
            }
            Console.WriteLine("\nTong So Chan La:{0}", sumChan);
            Console.WriteLine("Tong So Le La :{0}", sumLe);
        }
    }
}



Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-05-19 03:02:01




using System;

namespace Lession3
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = null;
            Console.Write("Nhap 1 chuoi bat ki: ");
            str = Console.ReadLine();
            Console.WriteLine("\n===============================================\n");
            //Lay tung ki tu cua chuoi str luu vao mang mang[];
            char[] mang = str.ToCharArray();

            int[] stt = new int[str.Length];

            //Lay ma cua cac ki tu luu vao mang stt[];
            Console.Write("Ma cua cac ki tu vua nhap vao la: ");
            for (int i = 0; i < str.Length; i++) {
                stt[i] = Convert.ToInt32(mang[i]);
                Console.Write("{0} ", stt[i]);
            }

            Console.WriteLine(" ");

            //Tinh va hien thi tong so chan, le cua mang stt[];
            sum(stt);

            //Sap xep theo thu tu tang dan mang stt[];
            sortASC(stt); 

            //Hien thi mang stt[] sau khi da sap xep tang dan;
            Console.Write("Mang ma cac ki tu sau khi sap xep theo thu tu tang dan: ");
            for (int i = 0; i< str.Length; i++)
            {
                Console.Write("{0} ", stt[i]);
            }
            //Convert mang stt da sap xep sang 1 chuoi moi va hien thi chuoi do ra man hinh;
            Console.Write("\nChuoi moi sau khi sap xep: ");
            for (int i = 0; i < str.Length; i++)
            {
                mang[i] = (char)stt[i];
                Console.Write("{0}", mang[i]);
            }

            Console.ReadKey();

            
        }

        static void sortASC(int[] arr )
        {
            int temp = arr[0];
            for(int i = 0; i < arr.Length -1; i++ )
            {
                for (int j = i + 1; j < arr.Length; j++) {
                if(arr[i] > arr[j])
                {
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                }
            }
            }
        }

        static void sum(int[] arr)
        {
            int tongch=0, tongle=0;
            for(int i = 0; i < arr.Length; i++)
            {
                if(arr[i] % 2 == 0)
                {
                    tongch += arr[i];
                }
                else
                {
                    tongle += arr[i];
                }
            }
            Console.WriteLine("Tong cac so chan trong mang tren la: {0}", tongch);
            Console.WriteLine("Tong cac so le trong mang tren la: {0}", tongle);
        }

        
    }
}



thienphu [T1907A]
thienphu

2020-05-19 02:22:19



 
using System;

namespace Lesson2
{
    class Program
    {
        static void Main(string[] args)
        {
			#region Tính tổng số chắc & Tổng số lẻ trong chuỗi - Lập Trình C# - Array Trong C#


            Console.WriteLine("Nhap 1 chuoi ki bat ki");
            string data = Console.ReadLine();
            Console.WriteLine("data = {0}", data);
            char[] charArr = data.ToCharArray();
            int index = charArr.Length;
            int[] intArr = new int[index];
            Console.WriteLine("In mang char");
            for (int i = 0; i < charArr.Length; i++)
            {
                Console.WriteLine("charArr[{0}] ={1}", i, charArr[i]);

                Console.WriteLine(" ASCII : " + Convert.ToInt32(charArr[i]));

                intArr[i] = Convert.ToInt32(charArr[i]);
            }

            for (int i = 0; i < intArr.Length; i++)
            {
                for (int j = i + 1; j < intArr.Length; j++)
                {
                    if (intArr[j] < intArr[i])
                    {

                        int tmp = intArr[i];
                        intArr[i] = intArr[j];
                        intArr[j] = tmp;
                    }
                }
            }
            Console.WriteLine("Mang sap xep theo thu tu tang dan ");
            int sumChan = 0;
            int sumLe = 0;
            char[] array3 = new char[index];
            for (int i = 0; i < intArr.Length; i++)
            {
                Console.WriteLine("intArr[{0}] ={1}", i, intArr[i]);
                array3[i] = Convert.ToChar(intArr[i]);
                if (intArr[i] % 2 == 0)
                {
                    sumChan += intArr[i];
                }
                else
                {
                    sumLe += intArr[i];
                }


            }
            //in ra tổng số chẵn sô lẻ
            Console.WriteLine("Tong cac so chan trong mang ={0}",sumChan);
            Console.WriteLine("Tong cac so le trong mang ={0}", sumLe);
            string newString = new string(array3);
            Console.WriteLine("new String ={0}",newString);
            

            

            #endregion
        }

    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-05-18 14:36:47



using System;

namespace kytu
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.Write("\nEnter String : ");
            string str = Console.ReadLine();
            int[] s = new int[str.Length];

            convertStr_Int(s,str);
            sum(s);
            sort(s);
            convertInt_Str(s);

            
            
        }
        static void sort(int[] s)
        {
            Console.WriteLine("\n sap xep : ");
            int N = s.Length;
            for (int i = 0; i < N - 1; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    if (s[i] > s[j])
                    {
                        int temp = s[i];
                        s[i] = s[j];
                        s[j] = temp;
                    }
                }

            }
            foreach (int n in s)
            {
                Console.Write("{0} ,",n);
            }
        }
        static void convertStr_Int(int[] s,string str)
        {
            int k = 0;
            foreach (char c in str)
            {
                s[k] = (int)c;
                Console.Write("{0} ,",s[k]);
                k++;
            }
        }
        static void convertInt_Str(int[] s)
        {
            Console.Write("\nnew string :  ");
           int k = 0;
            foreach (int n in s)
            {
                Console.Write((char)s[k]);
                k++;
            }
        }
        static void sum(int[] s)
        {
            int tongchan = 0, tongle = 0;
            foreach(int n in s)
            {
                if(n % 2 == 0)
                {
                    tongchan += n;
                }
                else
                {
                    tongle += n;
                }
                
            }
            Console.WriteLine("\nTong so chan : {0} \n Tong so le : {1}",tongchan,tongle);
        }
    }
}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-05-18 14:14:56



using System;

namespace project2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Moi ban nhap mang: ");
            string str = Console.ReadLine();
            char[] mang = str.ToCharArray();
            int[] number = new int[mang.Length];
            
            for (int i = 0; i < mang.Length; i++)
            {
                number[i] = (int) mang[i];
            }
            
            TinhTong(number);
            SapXep(mang,number);
        }

        public static void TinhTong(int [] number)
        {
            int tongChan = 0, tongLe = 0;
            for (int i = 0; i < number.Length; i++)
            {
                if (number[i] % 2 == 0)
                {
                    tongChan += number[i];
                }
                else
                {
                    tongLe += number[i];
                }
            }
            Console.Write("Tong so chan: {0}",tongChan);
            Console.WriteLine();
            Console.Write("Tong so chan: {0}",tongLe);
            Console.WriteLine();
        }

        public static void SapXep(char[] mang, int[] number)
        {
            int a;
            for (int i = 0; i < number.Length-1; i++)
            {
                for (int j = i+1; j < number.Length; j++)
                {
                    if (number[i] > number[j])
                    {
                        a = number[j];
                        number[j] = number[i];
                        number[i] = a;
                    }
                }
            }
            
            Console.Write("Mang khi sap xep lai: ");
            for (int i = 0; i < number.Length; i++)
            {
                mang[i] = (char) number[i];
                Console.Write(mang[i]);
            }
        }
        
    }
}