-5

I have 2 tasks, on C#

First is I need to reverse a string, I found some examples on google, this website but for some reason It didint worked out or I dont know how to use it correcly. Because I am very new at C#, just learning it.

So first one, lets say i have a string string A = "123456" and what I need is to write a function to get an output "654321"

If possible, when you give an answer, please be as detailed as possible, cuz this stuff, as I said said, is not easy for me right now :)

Next task is to make sum of string and int. Lets say i have string A = "123 and I would like to + 10 so the output would be A = "133"

Thank you all ! And again, pls, if you answering any of these, be detailed :)

DevyDev
  • 846
  • 3
  • 14
  • 37
  • 3
    Provide examples of what you have tried and specific error messages you're getting. Asking people on SO to write your code for you is generally frowned upon and will get your question downvoted, closed, and deleted. Especially when it sounds an awful lot like an interview or homework question. – Craig W. Feb 04 '15 at 20:00
  • `sounds like homework` also this is so simple that If you were to a google search you might slap yourself in the face by just how easy it is to type it in a google search... – MethodMan Feb 04 '15 at 20:01
  • You said you tried to implement this, please show what you have tried thus far. – Joe Brunscheon Feb 04 '15 at 20:03
  • Well, reverse it pretty easy: `"123456".Reverse()` – Magnus Feb 04 '15 at 20:06
  • First of all, not homework or smth, just for me. for first task I tried this **http://tipsandtricks.runicsoft.com/CSharp/StringReverse.html** and **http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string** But as I said, I don't get the meanings, what each command do, I am into this C# just ~3-2 days Edit: Even from what other people posted, I dont know what every single command means, its not enought just copy & paste it for me. thanks for minuses :) – DevyDev Feb 04 '15 at 20:06
  • @Magnus That doesn't give you a string. – Preston Guillot Feb 04 '15 at 20:07
  • @PrestonGuillot Ok, I guess there is a little more to it. `new string("123456".Reverse().ToArray())` – Magnus Feb 04 '15 at 20:10
  • this can be done even easier using a `List and the Join() Mehtod` `var lstRev = new List() { "123456" }; var reverseString = string.Join("",lstRev[0].Reverse().ToList());` – MethodMan Feb 04 '15 at 20:11
  • mm.. I will need a while, to know if its working, i hope it does ,so thank you for help :) – DevyDev Feb 04 '15 at 20:14

1 Answers1

1

To reverse:

public static string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

To add:

string A = "123";
int strA = int.Parse(A);
string result = (strA + 10).ToString();
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108