4 Replies - 1745 Views - Last Post: 20 May 2011 - 06:27 AM Rate Topic: -----

#1 aboodnet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 11

C#: Problem Reversing multiple lines from a text file.

Posted 18 May 2011 - 11:24 PM

Hello everyone..

I am trying to decode the text below by reversing it.

,sdreh ni dam og yeht taht nees eb lliw ti ;sdreh ni kniht neM“
”.eno yb eno dna ,ylwols sesnes rieht revocer ylno yeht elihw
yakcaM selrahC


I used this code to do it.

     String reed = File.ReadAllText("DecodeThis.txt");
           char[] array1 = reed.ToCharArray();                
           Array.Reverse(array1);
           Console.WriteLine(array1);
           Console.ReadLine();


The problem with this code is that it reverse not only the charactors but also the lines.

I want the outcome to be

" Men think in hards: it will be seen taht they go mad in herds,
while they only recover their senses slowly, and on by one.
Charles Mackay"

NOT


"Charles Mackay
while they only recover their senses slowly, and on by one.?
?Men think in hards: it will be seen taht they go mad in herds"


How can i do that? please help :(

Is This A Good Question/Topic? 0
  • +

Replies To: C#: Problem Reversing multiple lines from a text file.

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: C#: Problem Reversing multiple lines from a text file.

Posted 18 May 2011 - 11:40 PM

Read your file line by line instead of reading it all at once and apply the same logic
Was This Post Helpful? 0
  • +
  • -

#3 aboodnet  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-May 11

Re: C#: Problem Reversing multiple lines from a text file.

Posted 18 May 2011 - 11:47 PM

View PostIonut, on 18 May 2011 - 11:40 PM, said:

Read your file line by line instead of reading it all at once and apply the same logic



I know how to do it line by line using while loop but i was wondering if i could tweak my current code to achieve the same output as the while statement loop.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: C#: Problem Reversing multiple lines from a text file.

Posted 19 May 2011 - 05:04 AM

You could probably create an array of strings by splitting reed on the newline characters. You're still going to have to loop through it so you're only reversing the individual elements and not the array as a whole though.

This post has been edited by Nakor: 19 May 2011 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Re: C#: Problem Reversing multiple lines from a text file.

Posted 20 May 2011 - 06:27 AM

View Postaboodnet, on 18 May 2011 - 11:47 PM, said:

View PostIonut, on 18 May 2011 - 11:40 PM, said:

Read your file line by line instead of reading it all at once and apply the same logic



I know how to do it line by line using while loop but i was wondering if i could tweak my current code to achieve the same output as the while statement loop.


Instead of using ReadAllText function use the ReadAllLines functions.it returns a string array.You can reverse eac individual array using the Array.reverse function on each array line.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1