Reputation: 23
First, good day to all.
Excuse my bad English.
I have a Question, i want to make my first own program, this Program should have the following task:
You have as example this Hashsum:
9f73c507603e62c48926eb37f0f19f46
And the Program should convert it into this:
"9","c","8","e","d","1","5","d","0","b","e","b","e","5","c","1","2","6","f","2","3","3","9","5","b","f","8","0","4","8","d","8"
I don't ask without searching and trying by myself, but keep in mind im still a beginner, my " Solution " is this:
namespace Hash_Checker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
string myhash = Console.ReadLine();
string[] words = myhash.Split(' ');
Console.WriteLine("Modified Hashsum:");
foreach (var word in words)
{
System.Console.Write($"\"{word}\", ");
}
Console.ReadKey();
}
}
}
Ignore maybe the bad form, i will try to write it better later.
My 2 Problems are:
You must Enter the Hashsum like this:
9 f 7 3 c 5 0 7 6 0 3 e 6 2 c 4 8 9 2 6 e b 3 7 f 0 f 1 9 f 4 6
With delimiters, but i want it that you can input it without it, like this:
9f73c507603e62c48926eb37f0f19f46
But when you do that, it ends in this output:
"9f73c507603e62c48926eb37f0f19f46",
The Second Problem:
My Program make a " , " after the last Number, but i don't want that.
Example:
Should be:
"9","c","8","e","d","1","5","d","0","b","e","b","e","5","c","1","2","6","f","2","3","3","9","5","b","f","8","0","4","8","d","8"
But it is:
"9", "c", "8", "e", "d", "1", "5", "d", "0", "b", "e", "b", "e", "5", "c", "1", "2", "6", "f", "2", "3", "3", "9", "5", "b", "f", "8", "0", "4", "8", "d", "8",
The last , should be removed.
Anyone have a tip or a solution for me? Or it is little bit to hard for a beginner?
Thanks for all help from you!
Upvotes: 2
Views: 1877
Reputation: 5457
One (of many) ways to accomplish this would be to read your string in from input, select each character out of the string and surround it with quotations using LINQ, then join each of those character strings with string.Join()
as follows:
namespace Hash_Checker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
var input = Console.ReadLine();
//select each character from the string and turn
//each into a string that looks like "<character here>"
var characters = input.Select(x => string.Format("\"{0}\"", x));
//place comma in between each string containing "<character here>"
var formattedString = string.Join(",", characters);
System.Console.Write(formattedString);
Console.ReadKey();
}
}
}
Upvotes: 3
Reputation: 13756
you can loop thru the string because a string is an array of chars. so you can do this like this.
namespace Hash_Checker
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
string myhash = Console.ReadLine();
//replace all spaces
string words = myhash.Replace(" ", "");
Console.WriteLine("Modified Hashsum:");
StringBuilder sb = new StringBuilder();
//loop thru chars
foreach (var c in words)
{
sb.Append($"\"{c}\", ");
}
//trim the comma and space at the end of string and write it to console
Console.WriteLine(sb.ToString().TrimEnd(new char[] { ',', ' ' }));
Console.ReadKey();
}
}
}
Upvotes: 0
Reputation: 607
With minimum change to your code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
string myhash = Console.ReadLine();
var words = myhash.ToCharArray().Select(c => $"{c}");
Console.WriteLine("Modified Hashsum:");
foreach (var word in words)
{
System.Console.Write($"\"{word}\",");
}
Console.ReadKey();
}
}
ToCharArray()
gives you a char[]
with each individual char, and Select(c => $"{c}")
is the shortest way to convert the chars to strings.
I changed the variable type from string[]
to var
. In reality, the type is now an IEnumerable<string>
which works just as well for your loop.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
var words = Console.ReadLine().ToCharArray().Select(c => $"{c}");
Console.WriteLine("Modified Hashsum:");
Console.Write("\"" + words.Aggregate((c, n) => $"{c}\",\"{n}") + "\"");
Console.ReadKey();
}
}
This way you'll also get rid of the tailing ,
after your last character.
Upvotes: 0
Reputation: 164
Why not just have it go to a char array instead?
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
string myhash = Console.ReadLine();
var words = myhash.ToCharArray();
Console.WriteLine("Modified Hashsum:");
foreach (var word in words)
{
Console.Write($"\"{word}\", ");
}
Console.ReadKey();
}
EDIT: Like Dandre suggested this is how you do it converting it to a string array:
static void Main(string[] args)
{
Console.WriteLine("Enter Your Hashsum.");
string myhash = Console.ReadLine();
var charArr = myhash.ToCharArray();
var words = charArr.Select(s => s.ToString()).ToArray();
Console.WriteLine("Modified Hashsum:");
foreach (var word in words)
{
Console.Write($"\"{word}\", ");
}
Console.ReadKey();
}
Upvotes: 0
Reputation: 2173
I can point you to the ToCharArray
method that a string
has. This will already give you what you are looking for except that it is all char
instead of string
. You can easily do a Linq .Select(s => s.ToString())
if you want that array to be of string
.
So:
var str = "12345";
var charArr = str.ToCharArray();
var strArr = charArr.Select(s => s.ToString()).ToArray();
Upvotes: 0