C#小练习

请编写程序,将用户输入的字符串中的单词逆序输出

例如:

“你我 他” → “他 我你”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace test
{
   class Program
    {
       static void Main(string[] args)
       {
 
           string a;
           char[] b = null;
           int num = 0;
           Console.WriteLine("in put some words");
           a = Console.ReadLine();
           b = a.ToCharArray();
           for (int i = 0; i < b.Length; i++)
           {
                Console.Write("{0}",b[i]);
           }
           Array.Reverse(b);
           Console.WriteLine();
           for (int i = 0; i < b.Length; i++)
           {
                Console.Write("{0}",b[i]);
           }
           Console.WriteLine();
 
       }
    }
}


给定字符串“Thequick brown box jumped over the lazy dog. An apple a day keeps the doctor away.Can a fox and a dog be friends?”

统计单词“the”在字符串中出现的次数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CSharp_7_2
{
   class Program
    {
       static void Main(string[] args)
       {
           string a;
           int num = 0;
           Console.WriteLine("输入一串字符:");
            a = Console.ReadLine();
           a=a.ToLower();
         
           for (int i = 0; i < a.Length-2; i++)
           {
                char[] c=newchar[3]{a[i],a[i+1],a[i+2]};
             
                string b = new string(c);
                if (b.Equals("the"))
                {
                    num++;
                }
           }
           Console.WriteLine(num);
           Console.WriteLine();
       }
    }
}


输入任意字符串,统计其中元音字母('a'、'e'、'i'、'o'、'u')的出现次数和频率统计单词“the”在字符串中出现的次数和频率。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CSharp_7_3
{
   class Program
    {
       static void Main(string[] args)
       {
           Console.WriteLine("输入一字符串:");
           string instr;
           instr = Console.ReadLine();
           instr.ToCharArray();
           instr.ToLower();
           float[] compute = new float[6] { 0, 0, 0, 0, 0, 0 };
           for (int i = 0; i < instr.Length; i++)
           {
                switch (instr[i])
                {
                   case 'a':
                        compute[0]++;
                        break;
                    case 'e':
                        compute[1]++;
                        break;
                    case 'i':
                        compute[2]++;
                        break;
                    case 'o':
                        compute[3]++;
                        break;
                    case 'u':
                        compute[4]++;
                        break;
 
                    default:
                        break;
                }
 
           }
           for (int i = 0; i < instr.Length - 2; i++)
           {
                char[] c = new char[3] {instr[i], instr[i + 1], instr[i + 2] };
 
                string b = new string(c);
                if (b.Equals("the"))
                {
                    compute[5]++;
                }
           }
           Console.WriteLine("a,e,i,o,u,the 出现的次数与频率分别为:");
           for (int i = 0; i < compute.Length; i++)
           {
                Console.WriteLine("次数:{0,2},频率:{1:p1}", compute[i],compute[i] / instr.Length);
           }
           Console.WriteLine();
       }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值