Example:
string str = \"I am going to reverse myself.\";
string strrev = \"I ma gniog ot esrever .flesym\"; //An easy way to achieve this
A
you can use linq as
String newStr = new String( str.Reverse().ToArray() );
To reverse a string I use:
new String( word.Reverse().ToArray() )
The Reverse()
function is part of LINQ and works because String implements IEnumerable<char>
. Its result is another IEnumerable<char>
which now needs to be converted to string. You can do that by calling ToArray()
which gives a char[]
and then pass that into the constructor of string
.
So the complete code becomes:
string s="AB CD";
string reversed = String.Join(" ",
s.Split(' ')
.Select(word => new String( word.Reverse().ToArray() ) ));
Note that this code doesn't work well with certain unicode features. It has at least two problems:
char
s when UTF-16 encoded. Reversing them breaks the encoding. This is relatively easy to fix since there are just a limited number of characters initiation such a sequence(16 if I remember correctly) and this most likely won't be extended in future unicode versions.Well, here's a LINQ solution:
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray())));
If you're using .NET 3.5, you'll need to convert the reversed sequence to an array too:
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray()))
.ToArray());
In other words:
string(char[])
constructorToArray()
on the string sequence, as .NET 4 has more overloads availablestring.Join
on the result to put the reversed words back together again.Note that this way of reversing a string is somewhat cumbersome. It's easy to create an extension method to do it:
// Don't just call it Reverse as otherwise it conflicts with the LINQ version.
public static string ReverseText(this string text)
{
char[] chars = text.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
Note that this is still "wrong" in various ways - it doesn't cope with combining characters, surrogate pairs etc. It simply reverses the sequence of UTF-16 code units within the original string. Fine for playing around, but you need to understand why it's not a good idea to use it for real data.
static void Main(string[] args)
{
string str = "Hi how are you";
char[] char_arr= str.ToCharArray();
string finalstr = "";
string eachwords = "";
string tempreverseword = "";
int char_length = char_arr.Length;
for (int i = 0; i < char_arr.Length; i++)
{
if (char_arr[i].ToString() == " " || i == char_length-1)
{
if (i == char_length - 1)
{
eachwords += char_arr[i] + "";
}
char[] revchar_arr = eachwords.ToCharArray();
for (int j = revchar_arr.Length-1; j >=0; j--)
{
tempreverseword += revchar_arr[j];
}
finalstr += tempreverseword+" ";
tempreverseword = "";
eachwords = "";
}
else
{
eachwords += char_arr[i] + "";
}
}
Console.WriteLine(finalstr);
Console.ReadKey();
}
//Without Extension Methods Like: Split, ToCharArray, etc..
public string ReverseString(string str="Hai How Are You?"){
var FullRev="",
var wordRev="";
for(i=0;i<=str.length;i++){
if(str[i]==" " || i==str.length){
FullRev=FullRev+" "+wordRev; //FullRev=wordRev+" "+FullRev;
wordRev="";
}else{
wordRev=str[i]+wordRev;
}
}
return FullRev;
}
//Result "iaH woH erA ?uoY"
I used XOR for swapping from here http://en.wikipedia.org/wiki/XOR_swap_algorithm
X := X XOR Y
Y := X XOR Y
X := X XOR Y
the C# is:
public string ReverseWords(string str)
{
StringBuilder strrev = new StringBuilder();
StringBuilder reversedword = new StringBuilder();
foreach (var word in str.Split(' '))
{
char[] singlesentence = word.ToCharArray();
int j = singlesentence.Length / 2;
if (j > 0)
{
for (int i = singlesentence.Length - 1, c = 0; i == j; c = c + 1, i = i - 1)
{
singlesentence[c] = (char)(singlesentence[c] ^ singlesentence[i]);
singlesentence[i] = (char)(singlesentence[c] ^ singlesentence[i]);
singlesentence[c] = (char)(singlesentence[c] ^ singlesentence[i]);
}
}
strrev.Append(singlesentence);
strrev.Append(" ");
}
return strrev.ToString();
}