Easy way to cancel every word in a sentence

Example:

string str = "I am going to reverse myself.";
string strrev = "I ma gniog ot esrever .flesym"; //An easy way to achieve this

I think I need to iterate over each word, and then every letter of each word.

What I did works great. But I need an easy / short way.

C # CODE:

  string str = "I am going to reverse myself.";
  string strrev = "";

  foreach (var word in str.Split(' '))
  {
     string temp = "";
     foreach (var ch in word.ToCharArray())
     {
         temp = ch + temp;
     }
     strrev = strrev + temp + "";
  }
  Console.WriteLine(strrev);  //I ma gniog ot esrever .flesym
+6
source share
7 answers

Well, here is the LINQ solution:

var reversedWords = string.Join(" ",
      str.Split(' ')
         .Select(x => new String(x.Reverse().ToArray())));

If you are using .NET 3.5, you also need to convert the inverse sequence to an array:

var reversedWords = string.Join(" ",
      str.Split(' ')
         .Select(x => new String(x.Reverse().ToArray()))
         .ToArray());

In other words:

  • Divide by spaces
  • For each word, create a new word, treating the input as a sequence of characters, cancel this sequence, turn the result into an array, and then call the constructor string(char[])
  • ToArray() , .NET 4
  • string.Join , .

, . :

// 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);
}

, - "" - - , .. UTF-16 . , , .

+15

, :

new String( word.Reverse().ToArray() )

Reverse() LINQ , String IEnumerable<char>. IEnumerable<char>, . , ToArray(), char[], string.

, :

string s="AB CD";
string reversed = String.Join(" ",
    s.Split(' ')
     .Select(word => new String( word.Reverse().ToArray() ) ));

, Unicode. :

  • Unicode char UTF-16. . , , ​​ (16, ), , , Unicode.
  • . , , . , .
+5

Linq

String newStr = new String( str.Reverse().ToArray() );
0

1 -

  public static string reverseString(this string description) {

        char[] array = description.ToCharArray().Reverse().ToArray();

        return new string(array);
    }

2 -

    public static string reverseText(this string description) {

      string [] reversetext=  description.Split(' ').Select(i => i.ToString().reverseString()).Reverse().ToArray();

      return string.Join(" ", reversetext);
    }
0

I used XOR for sharing here http://en.wikipedia.org/wiki/XOR_swap_algorithm

X := X XOR Y
Y := X XOR Y
X := X XOR Y

WITH#:

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();
    }
0
source
//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"
0
source
public static void ReverseEachWordString(string abc)
        {
            int start_index = 0, end_index = abc.Length - 1;
            int i = 0, j = 0;
            char[] arr = abc.ToCharArray();
            try
            {
                while (start_index < end_index)
                {
                    if (arr[start_index] == ' ')
                    {
                        Console.WriteLine(arr[start_index]);
                        start_index++;
                        i = start_index;
                    }
                    else
                    {
                        if (arr[i] != ' ')
                        {
                            if (i == end_index)
                            {
                                i++;
                                for (j = i - 1; j >= start_index; j--)
                                {
                                    Console.WriteLine(arr[j]);
                                }
                                break;
                            }
                            else
                                i++;
                        }
                        else
                        {
                            for (j = i - 1; j >= start_index; j--)
                            {
                                Console.WriteLine(arr[j]);
                            }
                            i++;
                            start_index = i - 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }       
0
source

All Articles