1

I have a sentence like this (in Hebrew,RTL):

ואמר was here אוראל

Now, when I insert this into array I got:

  1. array[0] = אוראל
  2. array[1] = was
  3. array[2] = here
  4. array[3] = :ואמר

Now, because Hebrew is a RTL language I need to reverse the english letters and I need to switch the positions. Notice that I need to take care of any sentence (which means I can have more than two english words).

How do I get the result to be like the following?

ואמר ereh saw אוראל

I thought maybe to build any time a new string with just the english words,Reverse it and build the original string again, or maybe use in ref strings...


thanks for your helping but i still got a problem! i had splited the sentence as you said,i reversed the array and i got this:

לארוא ereh saw רמאו

after the step 2 the postions of the hebrew words are worng! in step 3 i reversed the hebrew words again and i got:

אוראל ereh saw ואמר

and i need to switch their position(one in one, as i said i can have a sentence with a lot of words..) so, i didn't understand how i "put the array string back together"(step 5)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Orel Gabay
  • 35
  • 6
  • 3
    Is it homework of a kind - mark with "homework" tag... It is also unclear what you have problem with - you listed several approaches but no question. – Alexei Levenkov Mar 08 '12 at 18:20

2 Answers2

5

Well, if you split the problem into pieces, it would look like this. You need to:

  1. Split a sentence into an array of strings
  2. reverse the Array
  3. Detect if a word is in Hebrew
  4. if it is not in Hebrew you need to Reverse the string
  5. now you just need to put the array of strings back together in one string and you're done!

EDIT: I misunderstood your problem. You could do something like this:

    public static string ProcessEnglishHebrewSentence(string sentence)
    {
        var ret = new List<string>();
        string[] words = sentence.Split(' ');

        var curHebrewList = new List<string>();
        var curEnglishList = new List<string>();
        bool curLangIsHebrew=false;

        foreach(var w in words)
        {
            if(IsHebrew(w) && curLangIsHebrew) // we have a word in Hebrew and the last word was in Hebrew too
            {
                curHebrewList.Add(w);
            }
            else if(IsHebrew(w) && !curLangIsHebrew) // we have a word in Hebrew and the last word was in English
            {
                if(curEnglishList.Any())            {
                    curEnglishList.Reverse();
                    ret.AddRange(curEnglishList);
                } // reverse current list of English words and add to List
                curEnglishList = new List<string>(); // create a new empty list for the next series of English words
                curHebrewList.Add(w);
                curLangIsHebrew=true; // set current language to Hebrew
            }
            else if(!IsHebrew(w) && !curLangIsHebrew) // we have a word in English and the last word was in English
            {
                curEnglishList.Add(new String(w.Reverse().ToArray())); // reverse and add it to the current series of English words
            }
            else if(!IsHebrew(w) && curLangIsHebrew) // we have a word in English and the last word was in Hebrew
            {
                if(curHebrewList.Any()) ret.AddRange(curHebrewList); // add current list of Hebrew words to List of Lists
                curHebrewList = new List<string>(); // create a new empty list for the next series of Hebrew words
                curEnglishList.Add(new string(w.Reverse().ToArray()));
                curLangIsHebrew=false; // set current language to English
            }
            else
            {
                throw new Exception("there should be no other case...");
            }
        }
        if(curHebrewList.Any()) ret.AddRange(curHebrewList);
        if(curEnglishList.Any())
        {
            curEnglishList.Reverse();
            ret.AddRange(curEnglishList);
        }

        return ret.Aggregate((a,b) => a + " " + b);
    }
Community
  • 1
  • 1
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
  • thanks for your helping but i still got a problem! i had splited the sentence as you said,i reversed the array and i got this: לארוא ereh saw רמאו after the step 2 the postions of the hebrew words are worng! in step 3 i reversed the hebrew words again and i got: אוראל ereh saw ואמר and i need to switch their position(one in one,as i said i can have a sentence with a lot of words..) so,i didnt understand how i "put the array string back together"(step 5) help! – Orel Gabay Mar 10 '12 at 18:34
  • Apologies, I misread your question: I thought you needed to reverse the whole sentence and make only the english words right-to-left. Now I understand the words in hebrew should maintain their place and the words in english should be reversed. What I don't understand is how this would work in the general case. For instance, say h1, h2 and h3 are words in hebrew and e1, e2, e3 are words in english: how would you process a sentence like h1, e1, e2, h2, h3, e3? Should it become h1, e2(backwards), e1(backwards), h2, h3, e3(backwards)? Or h1, e3(backwards), e2(backwards), h2, h3, e1(backwards)? – Paolo Falabella Mar 11 '12 at 08:09
  • yes it's will be: h1, e1, e2, h2, h3, e3 = h1, e2(backwards), e1(backwards), h2, h3, e3(backwards) – Orel Gabay Mar 11 '12 at 12:50
0

Paolo did the hard work of figuring out the algorithm:

  1. Split a sentence into an array of strings
  2. Reverse the Array
  3. If a word is not in Hebrew reverse it
  4. Join the strings

Here is a more elegant version using LINQ:

var result = Sentence
   .Split(' ')
   .Reverse()
   .Select(w => IsHebrew(w) ? w : new String(w.Reverse().ToArray())
   .Join(" ") 
cdiggins
  • 17,602
  • 7
  • 105
  • 102