0

I need to create a program that will read a line of text in which the words are separated by spaces and punctuations, and will display it in reverse order. The line of text can also include numbers, but if it does, the displayed reversed text must exlude text with word

The problem is, the instructions say i can use only basic functions from .net library and use of complex functions like - sorting, searching charaters and things like that is strictly forbidden. That means .Contains is not allowed. Can some1 please help me, how to write that using basic functions?

EXAMPLE Galaxy Omega123, strains purple color, due to its remoteness! ---> remoteness! its to due color, purple strains Galaxy

string reverseString = "";
        for (int j = separatedLine.Length -1; j >= 0; j--)
        {
            if (separatedLine[j].Contains('0') || separatedLine[j].Contains('1') || separatedLine[j].Contains('2') || separatedLine[j].Contains('3') || separatedLine[j].Contains('4') || separatedLine[j].Contains('5') || separatedLine[j].Contains('6') || separatedLine[j].Contains('7') || separatedLine[j].Contains('8') || separatedLine[j].Contains('9'))
            {
                separatedLine[j] = "";
            }
            else
            {
                reverseString = reverseString + separatedLine[j];
            }
        }
        return reverseString;
fkr
  • 155
  • 3
  • 9
  • 21
  • 3
    _"must exlude text with word"_ means: must exlude text with numbers? What are the allowed methods? – Tim Schmelter Jan 16 '14 at 12:23
  • 1
    Is that your homework??? Then sorry we'll not gonna help you for that... you need to do it by your own... – The Hungry Dictator Jan 16 '14 at 12:24
  • similar to http://stackoverflow.com/questions/1009160/reverse-the-ordering-of-words-in-a-string?rq=1 – Junaith Jan 16 '14 at 12:24
  • Also similar to http://stackoverflow.com/questions/228038 – Bridge Jan 16 '14 at 12:25
  • 1
    Sounds like homework for me. Anyway, the description is not quite clear, maybe you can provide a sample string for input and output to make it clearer? Do you need to reverse the whole string, or just the words reversed but in same order? – Oliver Friedrich Jan 16 '14 at 12:26
  • If you reverse "THIS SAMPLE SENTENCE" should the result be "SENTENCE SAMPLE THIS" or "ECNETNES ELPMAS SIHT", or something else? – Matthew Watson Jan 16 '14 at 12:34
  • @RonakBhatt It is okay to ask about homework here as far as I know. But it's a challenge for people who will answer because it will be much better if answer will help to learn without providing complete answer. – cnd Jan 16 '14 at 12:34
  • Could you put up some examples of input covering all the cases you're expected to handle, along with expected output? – Bridge Jan 16 '14 at 12:36
  • You can ask about homework, but see this page first: http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions – starsplusplus Jan 16 '14 at 12:50
  • @MatthewWatson - Galaxy Omega123, strains purple color, due to its remoteness! ---> remoteness! its to due color, purple strains Galaxy – fkr Jan 16 '14 at 14:15
  • @BeowulfOF please check my answer above – fkr Jan 16 '14 at 14:16
  • @Heather- agree with your point but before asking question here he should have to search it on google. As he will easily find the solution on that... – The Hungry Dictator Jan 17 '14 at 03:24

2 Answers2

1

Sorry all, I couldn't resist to have fun of using LINQ here, although the question is about only basic .NET methods. Hence I'm not feeling as doing someone else homework. Still it might be useful for others looking at the question.

string text = "Here is 1234 number";
string output = string.Join(" ", text.Split(' ')
                                     .Select(s => s.Any(c => Char.IsDigit(c)) 
                                                    ? s : new string(s.Reverse().ToArray())));
// output is "ereH si 1234 rebmun"
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
0

Typical solution is to Split and Join Reversed parts:

  // Put all possible separators into array
  String[] parts = separatedLine.Split(new Char[] {' ', ',', ';'});
  // "," is a separator to use
  return String.Join(",", parts.Reverse());

e.g. if

  separatedLine = "1 2;3,4"; the outcome is "4,3,2,1"

If you're resticted in using Split, Linq etc. the solution could be kind of emulation:

  List<String> parts = new List<String>();
  StringBuilder part = new StringBuilder();

  foreach(Char ch in separatedLine) 
    // Put all possible separators into condition
    if ((ch == ' ') || (ch == ',') || (ch == ';')) {
      parts.Add(part);
      parts.Length = 0;
    }
    else 
      part.Add(ch);  

  parts.Add(part); // <- Do not forget to add the last one   

  StringBuilder result = new StringBuilder();

  for (int i = parts.Count - 1; i >= 0; --i) {
    if (i < parts.Count - 1)
      result.Add(','); // <- Separator to use

    result.Add(parts[i]);
  }

  return result.ToString();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215