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)

Trần Việt Đức Anh [C2010L]
Trần Việt Đức Anh

2021-10-01 11:55:13

#Ex1423


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

Yêu cầu thực hiện như sau. Tạo 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
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace Ex1423
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            byte[] ascii = Encoding.ASCII.GetBytes(str);
            List<int> array = new List<int>();
            foreach (byte b in ascii)
            {
                array.Add(b);
            }
            Console.WriteLine("Tong cac so chan la: {0}", TongChan(array));
            Console.WriteLine("Tong cac so le la: {0}", TongLe(array));
            array.Sort();
            Console.Write("Day sau khi sap xep tang dan la: ");
            for (int i = 0; i < array.Count; i++)
            {
                Console.Write("{0} ", array[i]);
            }
            Console.WriteLine();
            Console.WriteLine("Chuoi sau khi sap xep mang la: {0}", Chuoi(array));
            Console.ReadKey();
        }

        static int TongChan(List<int> array)
        {
            int sum = 0;
            for (int i = 0; i < array.Count; i++)
            {
                if (array[i] % 2 == 0) sum += array[i];
            }
            return sum;
        }
        static int TongLe(List<int> array)
        {
            int sum = 0;
            for (int i = 0; i < array.Count; i++)
            {
                if (array[i] % 2 != 0) sum += array[i];
            }
            return sum;
        }
        static string Chuoi(List<int> array)
        {
            string str = "";
            for (int i = 0; i < array.Count; i++)
            {
                str += ((char)(array[i]));
            }
            return str;
        }
    }
}



Phạm Đăng Khoa [community,C2010L]
Phạm Đăng Khoa

2021-09-30 13:56:50



using System;

namespace Lesson01
{
    class Program
    {
        static void Main(string[] args)
        {
            string a;
            Console.WriteLine("Nhap vao chuoi: ");
            a = Console.ReadLine();
            char[] chara = a.ToCharArray();
            int[] n = new int[chara.Length];
            for (int i= 0; i< chara.Length; i++)
            {
                n[i] = (int)chara[i];
            }
            int tongChan = 0;
            int tongLe = 0;
            for (int i = 0; i < n.Length; i++)
            {
                if (n[i] % 2 == 0)
                {
                    tongChan += n[i];
                }
                else
                {
                    tongLe += n[i];
                }
            }
            Console.WriteLine("Tong le la: " + tongLe);
            Console.WriteLine("Tong chan la: " + tongChan);
            int kt = 0;
            for (int i = 0; i < n.Length; i++)
            {
                for (int j = i + 1; j < n.Length; j++)
                {
                    if (n[i] > n[j])
                    {
                        kt = n[i];
                        n[i] = n[j];
                        n[j] = kt;
                    }
                }
            }
            Console.WriteLine("Cac so sau khi sap xep la: ");
            for (int i = 0; i < n.Length; i++)
            {
                Console.WriteLine(n[i] + " ");
            }
            Console.WriteLine("Tao mang theo chuoi: ");
            for (int i = 0; i < n.Length; i++)
            {
                chara[i] = (char)n[i];
                Console.Write( chara[i] + " ");
            }


        }
    }
}



Kim Văn Thiết [community,AAHN-C2009G]
Kim Văn Thiết

2021-09-23 05:27:32





 static void bai2()
        {
            Console.Write("Enter String:");
            string str =  Console.ReadLine();
            int TongChan = 0;
            int TongLe = 0;
            
            int[] arr = new int[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                arr[i] = (int)str[i];

                if (arr[i] % 2 == 0) // tong so chan, so le
                {
                    TongChan += arr[i];
                }
                else
                {
                    TongLe += arr[i];
                }

                
            }
            Console.WriteLine("Tong chan : {0}",TongChan);
            Console.WriteLine("Tong le : {0}", TongLe);
            for ( int i = 0; i < arr.Length; i++)
            {
                Array.Sort(arr);
                Console.Write("{0}  ", arr[i]);
                
                
            }
            string result = string.Join(",", arr);
            Console.WriteLine();
            Console.Write("Chuyen ve String : {0}",result);
        }



Hoàng Thiện Thanh [community,AAHN-C2009G]
Hoàng Thiện Thanh

2021-09-22 02:31:30


#Program.cs


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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter any words~");
            string str = Console.ReadLine();
            string result = "";
            byte[] ascii = Encoding.ASCII.GetBytes(str);
            //Console.WriteLine("{0}", ascii);
            int count = 0, sumEven = 0, sumOdd = 0;
            for(int i = 0; i < ascii.Length; i++)
            {
                count += ascii[i];
                if(count % 2 == 0)
                {
                    sumEven += count;
                } else
                {
                    sumOdd += count;
                }
            }
            Console.WriteLine("Sum of even: {0}", sumEven);
            Console.WriteLine("Sum of odd: {0}", sumOdd);
            List<int> NumericList = new List<int>();
            foreach (int num in ascii)
            {
                NumericList.Add(num);
            }
            NumericList.Sort();
            Console.WriteLine("Each ASCII of the charactes in the word sorted: ");
            foreach(int num in NumericList)
            {
                result += Convert.ToChar(num);
                Console.Write(num + " ");
            }
            Console.WriteLine("\nConverted String: {0}", result);
        }
    }
}



Nguyễn Việt Hoàng [community,AAHN-C2009G]
Nguyễn Việt Hoàng

2021-09-21 18:49:33


#Program.cs


using System;
using System.Collections.Generic;

namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter array :");
            string a = Console.ReadLine();
            char[] arr = a.ToCharArray();
            int count = 0, sumC = 0, sumL = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                count = arr[i];
                if (count % 2 == 0)
                {
                    sumC += count;
                }
                else
                {
                    sumL += count;
                }
            }
            List<int> saveNum = new List<int>();
            foreach(int c in arr)
            {
                saveNum.Add(c);
            }
            Console.WriteLine("Sum of even : {0}", sumC);
            Console.WriteLine("Sum of odd : {0}", sumL);
            saveNum.Sort();
            Console.Write("Sort by acs: ");
            foreach (int print in saveNum)
            {
            Console.Write(print + " ");
            }
            String afterPr = "";
            foreach(int prt in saveNum)
            {
                afterPr +=Convert.ToChar(prt);
            }
            Console.WriteLine("");
            Console.WriteLine("After sort by acs :{0}", afterPr);


        }
    }
}



Do Trung Duc [T2008A]
Do Trung Duc

2021-05-18 05:29:00



using System;
using System.Collections.Generic;
using System.Text;

namespace Lesson2.TinhTongChanLeTrongChuoi
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Nhap chuoi can them");
            string str = Console.ReadLine();
            byte[] ascii = Encoding.ASCII.GetBytes(str);
            Console.WriteLine("{0}",ascii);

            List<int> Array = new List<int>();
            foreach(byte num in ascii)
            {
                Array.Add(num);
            }

            Console.WriteLine("Tong cac so chan la {0}", TongChan(Array));
            Console.WriteLine("Tong cac so le la {0}", TongLe(Array));

            Array.Sort();
            Console.WriteLine("Day sau khi sap xep tang dan la");
            foreach (byte num in ascii)
            {
                Console.WriteLine("{0}", num);
            }

            Console.WriteLine("Chuoi sau khi duoc chuyen doi la: {0}", Chuoi(Array));

        }

        static int TongChan(List<int> Array)
        {
            int SumChan = 0;
            foreach(int num in Array)
            {
                if (num % 2 ==0)
                {
                    SumChan += num;
                }
            }
            return SumChan;
        }

        static int TongLe(List<int> Array)
        {
            int SumLe = 0;
            foreach (int num in Array)
            {
                if (num % 2 != 0)
                {
                    SumLe += num;
                }
            }
            return SumLe;
        }

        static string Chuoi(List<int> Array)
        {
            string Chuoi = "";
            foreach (int num in Array)
            {
                Chuoi = Chuoi + Convert.ToChar(num);
            }
            return Chuoi;
        }


    }
}



vuong huu phu [T2008A]
vuong huu phu

2021-05-17 12:07:43



using System;

namespace tinhmakitu
{
    class Program
    {
        static void Main(string[] args)
        {
            
            String a;
            Console.WriteLine("Nhap chuoi can tinh ");
            a = Console.ReadLine();
            char[] chara = a.ToCharArray();
            int[] n = new int[chara.Length];
            for (int i = 0; i < chara.Length; i++) {
                n[i] = (int)chara[i];
            }
            int tongchan = 0;
            int tongle = 0;
            for (int i = 0; i < n.Length; i++) {
                if (n[i] % 2 == 0)
                {
                    tongchan += n[i];
                }
                else {
                    tongle += n[i];
                }
            }
            Console.WriteLine("Tong chan = " + tongchan);
            Console.WriteLine("Tong le = " + tongle);
            int kt = 0;
            for (int i = 0; i < n.Length; i++) {
                for (int j = i+1; j < n.Length; j++) {
                    if (n[i] > n[j]) { 
                    kt = n[i];
                    n[i] = n[j];
                    n[j] = kt;
                }}
            }
            for (int i = 0; i < n.Length; i++) {
                Console.Write(n[i] + " ");
            }
            for (int i = 0; i < n.Length; i++)
            {
                chara[i] = (char)n[i];
            Console.Write(chara[i] + " ");
            }
            


        }
    }
}



Trần Văn Lâm [T2008A]
Trần Văn Lâm

2021-05-17 08:28:51



namespace Les2Bt2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Nhap so phan tu trong mang:");
            int n = Convert.ToInt32(Console.ReadLine());

            int[] arr = new int[n];
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine($"Nhap phan tu thu {i}: ");
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Mang truoc sap xep:");
            foreach(int x in arr)
            {
                Console.Write(x + "");
            }
            Console.WriteLine();
            Console.WriteLine("Mang sau sap xep tang dan:");
            Array.Sort(arr);
            foreach (int x in arr)
            {
                Console.Write(x + "");
            }
            Console.WriteLine();

        }
    }
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-05-17 08:27:52



using System;
using System.Collections.Generic;
using System.Text;

namespace TongChanLe
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            byte[] ascii = Encoding.ASCII.GetBytes(str);
            List<int> array = new List<int>();
            foreach(byte b in ascii)
            {
                array.Add(b);
            }
            Console.WriteLine("Tong cac so chan la: {0}", TongChan(array));
            Console.WriteLine("Tong cac so le la: {0}", TongLe(array));
            array.Sort();
            Console.Write("Day sau khi sap xep tang dan la: ");
            for(int i = 0; i < array.Count; i++)
            {
                Console.Write("{0} ", array[i]);
            }
            Console.WriteLine();
            Console.WriteLine("Chuoi sau khi sap xep mang la: {0}",Chuoi(array));
        }

        static int TongChan(List<int> array)
        {
            int sum = 0;
            for(int i = 0; i < array.Count; i++)
            {
                if (array[i] % 2 == 0) sum += array[i];
            }
            return sum;
        }
        static int TongLe(List<int> array)
        {
            int sum = 0;
            for (int i = 0; i < array.Count; i++)
            {
                if (array[i] % 2 != 0) sum += array[i];
            }
            return sum;
        }
        static string Chuoi(List<int> array)
        {
            string str = "";
            for (int i = 0; i < array.Count; i++)
            {
                str += ((char)(array[i]));
            }
            return str;
        }
    }
}



trung [C1907L]
trung

2020-06-25 14:07:36



using System;

namespace C_
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            Console.WriteLine("Input string");
            str = Console.ReadLine();
            int[] arr = new int[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                arr[i] = (int)str[i];
            }

            int oddSum, evenSum;
            oddSum = 0;
            evenSum = 0;
            for (int i = 0; i < str.Length; i++)
            {
                if (arr[i] % 2 == 0)
                {
                    evenSum += arr[i];
                }
                else
                {
                    oddSum += arr[i];
                }
            }

            Console.WriteLine("Sum of even number: {0}",evenSum);
            Console.WriteLine("Sum of odd number: {0}",oddSum);
            
            for (int i = 0; i < str.Length-1; i++)
            {
                for (int j = i+1; j < str.Length; j++)
                {
                    if (arr[i] > arr[j])
                    {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }

            string reStr = "";
            for (int i = 0; i < str.Length; i++)
            {
                reStr += (char) arr[i];
            }
            Console.WriteLine(reStr);
        }
    }
}