Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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

C#
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();           
        }
    }
}
Posted
Updated 28-Feb-14 7:17am
v2
Comments
Sergey Alexandrovich Kryukov 28-Feb-14 14:05pm    
This is not programming way of doing things. Anyway, what's your problem?
—SA
Richard MacCutchan 28-Feb-14 14:08pm    
Good grief!

I answered a similar question earlier today, looks like this week's homework.
Sergey Alexandrovich Kryukov 28-Feb-14 14:09pm    
Ah, assigning tasks to the crowd, how nice!
—SA
AjitRaje 3-Mar-14 0:28am    
thanks
Richard MacCutchan 28-Feb-14 14:10pm    
Look at the methods whose name begins with Is at http://msdn.microsoft.com/en-us/library/system.char.aspx.

Use below function;

C#
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();
        }


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!
 
Share this answer
 
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:
C#
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:
C#
if (b == 32)
{
    Console.Write(" "+b);
}
if (IsAlpha(c))
{
    a = Convert.ToInt32(str[i] - 64);


    if (a < 10)
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:
C#
private bool IsAlpha(char c)
   {
   return c >= 'A' && c <= 'Z';
   }
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:
C#
if (Char.IsLetter(c))
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:
C#
private int ToNumber (char c)
   {
   return (int) c - (int) 'A';
   }
'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:
C#
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!
 
Share this answer
 
Comments
BillWoodruff 28-Feb-14 15:14pm    
+5 Viva Education !
Maarten Kools 28-Feb-14 16:12pm    
+5 Great explanation, good of you for taking the time to do that!
AjitRaje 3-Mar-14 0:33am    
Thanks
OriginalGriff 3-Mar-14 5:01am    
You're welcome!
Let us know how it works out!
CPallini 3-Mar-14 4:39am    
5.
C#
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]);
                            }
                        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900