如何在c#中打印abcde..z 01 02 03..26 [英] how to print abcde..z 01 02 03..26 in c#

查看:57
本文介绍了如何在c#中打印abcde..z 01 02 03..26的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要打印Hello world

就像A bc d..z

01 02 03..26 char的空间值

i这样做但是如果我们输入01 02 03..26然后我想打印字符abc d..z

但是如果我不知道uerr可以进入哪个值

用户输入值(考虑下面任何一个值)取决于用户

如果我输入01然后我想作为一个

如果我输入abcd .. z然后我想要

01 02 03 04 ... 26

我的代码是



want to print Hello world
like A b c d..z
01 02 03..26 values of char with space
i did this but if we enter 01 02 03..26 then i want to print char a b c d..z
but if i don't know the which value uerr can enter
User enter value (consider values as any one of below)depend on user
if i enter 01 then i want to ans as a
if i enter abcd..z then i want
01 02 03 04...26
my code is

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

namespace StringManipulation
{
    
    class Program
    {
         static int a,b;
        static void Main(string[] args)
        {
            
            string str;
            int [] arr=new int[100];
            Console.WriteLine("Enter The String");
            str = Console.ReadLine();
            Console.WriteLine();      
            for (int i = 0;i<str.length;>            {
                Console.Write(str[i]);
            }
            Console.WriteLine();
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                b = Convert.ToInt32(str[i]);
                if (b == 32)
                {
                    Console.Write(" "+b);
                }
                if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'J' || c == 'K' || c == 'L' || c == 'M' || c == 'N' || c == 'O' || c == 'P' ||
                 c == 'Q' || c == 'R' || c == 'S' || c == 'T' || c == 'U' || c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z')
                {
                    a = Convert.ToInt32(str[i] - 64);
                    
                    
                    if (a < 10)
                    {
                        Console.Write(" 0" + Convert.ToInt32(str[i] - 64));
                    }
                    else
                    {
                        Console.Write(" " + Convert.ToInt32(str[i] - 64));
                        arr[i] = Convert.ToInt32(str[i]);
                    }
                }

                else if (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' || c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' ||
                   c == 'q' || c == 'r' || c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w' || c == 'x' || c == 'y' || c == 'z')

                //else if ((c == 'a') || (c == 'z'))
                {
                    a = Convert.ToInt32(str[i] - 96);
                    if (a < 10)
                    {
                        Console.Write(" 0" + Convert.ToInt32(str[i] - 96));
                    }
                    else
                    {
                        Console.Write(" " + Convert.ToInt32(str[i] - 96));
                        arr[i] = Convert.ToInt32(str[i]);
                    }
                }
                
            }
            Console.WriteLine();
            Console.WriteLine("Reverce String");
            for (int i = (str.Length - 1); i >= 0; i--)
            {
                Console.Write(str[i]);
            }
            Console.WriteLine();

            for (int i = (str.Length - 1); i >= 0; i--)
            {

                char c = str[i];
                b = Convert.ToInt32(str[i]);
                if (b == 32)
                {
                    Console.Write(" " + b);
                }
                //if ((c == 'A') && (c == 'Z'))
                if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'J' || c == 'K' || c == 'L' || c == 'M' || c == 'N' || c == 'O' || c == 'P' ||
                    c == 'Q' || c == 'R' || c == 'S' || c == 'T' || c == 'U' || c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z')
                {
                    a = Convert.ToInt32(str[i] - 64);
                    if (a < 10)
                    {
                        Console.Write(" 0" + Convert.ToInt32(str[i] - 64));
                    }
                    else
                    {

                        Console.Write("  " + Convert.ToInt32(str[i] - 64));
                    }
                }

               // else if ((c == 'a') && (c == 'z'))
                    

                        else if (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' || c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' ||
                           c == 'q' || c == 'r' || c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w' || c == 'x' || c == 'y' || c == 'z')
                        {
                            a = Convert.ToInt32(str[i] - 96);
                        if (a < 10)
                        {
                            Console.Write(" 0" + Convert.ToInt32(str[i] - 96));
                        }
                        else
                        {
                            Console.Write(" " + Convert.ToInt32(str[i] - 96));
                        }
                        }
                    }
                
            
            
            Console.WriteLine();
            Console.WriteLine("Reverce String");
           // for (int i = arr.Length-1; i >=0; i--)
           // {
           //     int c = arr[i];
           //     if (c == 1 || c == 2 || c == 2 || c == 4 || c == 5 || c == 6 || c == 7 || c == 8 || c == 9 || c == 10 || c == 11 || c == 12 || c == 13 || c == 14 || c == 15 || c == 16 ||
           //         c == 17 || c == 18 || c == 19 || c == 20 || c == 21 || c == 22 || c == 23 || c == 24 || c == 25 || c == 26)                                 
           //         Console.Write(" " + Convert.ToChar(arr[i] + 64));
                    
           //}


            Console.WriteLine("Enter element");
            int [] arr1 = new int[str.Length];
            
            for (int i = 0; i < str.Length; i++)
            {
               
                arr1[i] = Convert.ToInt32(Console.ReadLine());
            }
            for (int i =0; i<arr1.length;>            {
                if (arr1[i] == 32)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.Write(Convert.ToChar(arr1[i] + 64));
                }
            }
            
                Console.ReadLine();           
        }
    }
}

推荐答案

使用以下功能; < br $>


Use below function;

public string GetMappedString(string input)
        {
            Dictionary<string, string> datas = new Dictionary<string, string>();
            datas.Add("01", "A");
            datas.Add("02", "B");
            datas.Add("03", "C");
            datas.Add("04", "D");
            datas.Add("05", "E");
            datas.Add("06", "F");
            datas.Add("07", "G");

            string[] seperated = input.Split(' ');
            StringBuilder output = new StringBuilder();
            foreach (string character in seperated)
            {
              var elementBasedonKey=  datas.SingleOrDefault(x => x.Key == character);
              if (elementBasedonKey.Key == null)
              {
                  var elementBasedonValue = datas.SingleOrDefault(x => x.Value == character);
                  if (elementBasedonValue.Key != null)
                  {
                      output.Append(" " + elementBasedonValue.Key);
                  }
              }
              else
              {
                  output.Append(" " + elementBasedonKey.Value);
              }
            }
            return output.ToString();
        }





调用此函数如下;

GetMappedString(01 02 03 04 07 );



你甚至可以使用如下整数和字符的组合;

GetMappedString(01 C 03 04 A ); - 仍然有效!



Call this function as below;
GetMappedString("01 02 03 04 07");

You can even use a combination of both integers and characters as below;
GetMappedString("01 C 03 04 A"); -- Still Works!


首先要做的事情 - 我知道你是初学者,但是......你可以采取一些措施来改善它,并使它更具可读性。



首先查看它,看看你是否可以发现你做过几次的任何事情...比如如果测试例如!我的话,但这有点像眼睛,也许很难打字。

所以让我们把它移到一个单独的方法:

First things first - I know you are a beginner, but...there are some things you can do to improve that, and make it a lot more readable.

Start by looking at it, and see if you can spot anything you do several times...like your if test for example! My word, but that's a bit of an eyesore and probably hard to type as well.
So let's move that to a separate method:
private bool IsAlpha(char c)
   {
   return c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'J' || c == 'K' || c == 'L' || c == 'M' || c == 'N' || c == 'O' || c == 'P' ||
                 c == 'Q' || c == 'R' || c == 'S' || c == 'T' || c == 'U' || c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z';
   }

现在,我们可以在您的代码中使用它:

Now, we can use this in your code:

if (b == 32)
{
    Console.Write(" "+b);
}
if (IsAlpha(c))
{
    a = Convert.ToInt32(str[i] - 64);


    if (a < 10)

这更具可读性了!

真的很奇怪,因为你依赖'B'是一个在你的转换中大于'A',但你在测试这是在'A'和'Z'之间时是否忽略了它? - 您可以将IsAlpha重写为:

That's a bit more readable!
It's odd really, because you are relying on 'B' being one bigger than 'A' in your conversion, but you ignored that when you tested for "Is this between 'A' and 'Z'?" - you could have rewritten IsAlpha as:

private bool IsAlpha(char c)
   {
   return c >= 'A' && c <= 'Z';
   }

它会产生同样的效果!

实际上,我们不需要编写自己的IsAlpha方法:.NET为我们提供了一个。

我们可以说:

And it would have the same effect!
As it happens, we don't need to write our own IsAlpha method: .NET provides us with one.
We can just say:

if (Char.IsLetter(c))

我们得到相同的效果(但是因为它支持重音也有更好的值范围)



啊,但我可以听到你说:但我需要不同的大写和小写!

嗯,是的,你这样做 - 但是又一次,不,你没有。

因为你将字符转换为整数的方式(反之亦然)也可以更简单。



当您将'A'转换为数字时,您将其转换为01并且您将其转换为a - 它是01好。那么为什么不将所有小写字符转换为大写?那么你只需要查看一个字符范围!

.NET也为我们做了这些!有两种方法:整个字符串可以用string.ToUpper变成大写,或者你可以用char.ToUpper逐个字符地完成。整个字符串更容易!



现在,通过将转换代码转换为另一种方法,让我们再次阅读更容易:

And we get the same effect (but with a much better range of values as it supports accents as well)

Ah, but I can hear you saying already: "But I need upper and lowercase differently!"
Well, yes, you do - but there again, no, you don't.
Because the way you are converting characters to integers (and vice versa) could be simpler as well.

When you convert 'A' to a number, you are translating it to "01" and you do the same with 'a' - it's a "01" as well. So why not just convert all lowercase character to uppercase? Then you only have to look at one range of characters!
And .NET does that for us as well! There are two ways: the whole string can become uppercase with string.ToUpper, or you can do it character by character with char.ToUpper. The whole string is easier!

Now, let's make it easier to read again, by taking the conversion code out into another method:

private int ToNumber (char c)
   {
   return (int) c - (int) 'A';
   }

'A'只是你的常数值64,但是作为一个字符保留,所以代码更明显。



我做的下一个更改是不使用进行循环 - 我会使用 foreach 代替。 br />
这使整个代码更简单,更容易阅读:

'A' is just your constant value 64, but left as a character so the code is more obvious.

The next change I'd make is to not use a for loop - I'd use a foreach instead.
This makes the whole code a lot simpler, and much easier to read:

str = str.ToUpper();
foreach (char each in str)
    {
    if (char.IsLetter(each))
        {
        int value = (int)each - (int)'A' + 1;
        Console.Write("{0:00} ", value);
        }
    }
Console.WriteLine();

替换第一段的整个代码...如果你把它作为一个方法,你就可以把它全部传递给你的普通字符串,反过来,然后传递反转字符串到同样的方法!



我会在那里停下来,因为我现在可能还不够!

试一试,想一想,看看你是否可以将这些内容应用到将其转换回字母部分。我想你可能会这样!

Replaces your whole code for the first segment...and if you make this a method, you can just all it and pass your "normal" string, reverse that, and pass the reversed string to the same method!

I'm going to stop there, as I't probably enough for you at the moment!
Have a try, and a think, and see if you can apply any of this to your "convert it back to letters" part. I think you probably can!


class Program
        {
            static int a, b;

            static void Main(string[] args)
            {

                string str;
                int[] arr = new int[100];
                int start = 0, end = 2, pt,count=1;
                Console.WriteLine("Enter The Number or String");
                str = Console.ReadLine();
                //int no=Convert.ToInt32(str.Substring(0,2);

                Console.WriteLine();
                if (str[0] >= 48 && str[0] <= 57)
                {

                    for (int i = 0; i < str.Length; i = i + 2)
                    {

                        if (str[i] >= 31 && str[i] <= 47 || str[i] >= 58 && str[i] <= 64)
                        {//0+2=2+2=4+2=6
                            Console.Write(" " +Convert.ToChar(str[i]));
                            start = start + 1;
                            --i;
                            ++count;
                        }
                        else
                        {

                            pt = Convert.ToInt32(str.Substring(start, end));
                            start = start + 2;
                            if (pt >= 31 && pt <= 47 || pt >= 58 && pt <= 64)
                            {//0+2=2+2=4+2=6
                                Console.Write(" " + Convert.ToChar(pt));

                            }
                            else
                            {
                                Console.Write(" " + Convert.ToChar(64 + pt));
                            }
                        }

                    }

                }

                else
                {
                    for (int i = 0; i < str.Length; i++)
                    {
                        if (str[i] >= 31 && str[i] <= 47 || str[i] >= 58 && str[i] <= 64 || str[i] >= 48 && str[i] <= 57)
                        {
                            Console.Write(" " + Convert.ToInt32(str[i]));
                        }
                        if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= 'a' && str[i] <= 'z')
                        {
                            a = Convert.ToInt32(str[i] % 32);
                            if (a < 10)
                            {
                                Console.Write(" 0" + Convert.ToInt32(str[i] % 32));
                            }
                            else
                            {
                                Console.Write(" " + Convert.ToInt32(str[i] % 32));
                                // arr[i] = Convert.ToInt32(str[i]);
                            }
                        }
                    }

                    Console.WriteLine();
                    Console.WriteLine("Reverce String");
                    for (int i = (str.Length - 1); i >= 0; i--)
                    {
                        Console.Write(str[i]);

                    }
                    Console.WriteLine();

                    for (int i = (str.Length - 1); i >= 0; i--)
                    {

                        if (str[i] >= 31 && str[i] <= 47 || str[i] >= 58 && str[i] <= 64)
                        {
                            Console.Write(" " + Convert.ToInt32(str[i]));
                        }
                        if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= 'a' && str[i] <= 'z')
                        {
                            a = Convert.ToInt32(str[i] % 32);


                            if (a < 10)
                            {
                                Console.Write(" 0" + Convert.ToInt32(str[i] % 32));
                            }
                            else
                            {
                                Console.Write(" " + Convert.ToInt32(str[i] % 32));
                                arr[i] = Convert.ToInt32(str[i]);
                            }
                        }


这篇关于如何在c#中打印abcde..z 01 02 03..26的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆