Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working in my project and one of its functionalities is to reverse only text (The number should stay as its). For example the text This is a test file becomes
elif tset a si sihT. Another example which contains digits USA 123 UAE should become EAU 123 ASU
I write the following program but the result is not correct especially for the lines that contain numbers (the problem occurs when English text in the same line separated by numbers). For example, I got USA 123 UAE instead of the correct result EAU 123 ASU. Also the sentence Kuwait Iraq 784 Bahrain should be niarhaB 784 qarI tiawuK but I got the original one.
So any help or suggestions
VB
FileName = "C:\Users\PC\Desktop\test.txt"
        Dim AllTextFile As String = File.ReadAllText(FileName)
        Dim objReader As New System.IO.StreamReader(FileName)
        Dim seperatedWordsArray As String()
        Dim allTextLines As String = ""
        Dim tempTextLine As String = ""
        Dim englishSentence As String = ""
        Dim someSentences As New List(Of String)
        Do While objReader.Peek() <> -1
            TextLine = objReader.ReadLine()
            tempTextLine = TextLine
            englishSentence = ""
            seperatedWordsArray = Regex.Split(TextLine, " "c)
            For Each word As String In seperatedWordsArray
                If (Regex.IsMatch(word, "^[a-zA-Z,.:]*$")) Then
                    englishSentence = englishSentence & word & " "
                End If
            Next

            englishSentence = englishSentence.TrimEnd(" ") 'Remove the last space that added by the previous sentence englishSentence = englishSentence & word & " "
            If (englishSentence.Length > 0) Then
                tempTextLine = tempTextLine.Replace(englishSentence, StrReverse(englishSentence))
            End If
            allTextLines = allTextLines & tempTextLine & vbNewLine
        Loop
        TextBox2.Text = allTextLines


I have tried my program using the following text:
This is a test file
USA 123 UAE
Trump Tower NY 667
123 abcdef ABCDEF
Kuwait Iraq 784 Bahrain
A sentence is a group of words that expresses a statement, question, command or exclamation
111 Sentence 454 Different Types 777:
Statement, Question Type, Command Type, Exclamation Type


What I have tried:

I wrote a small program but the result is wrong
Posted
Updated 8-Dec-16 15:46pm
Comments
Richard MacCutchan 8-Dec-16 13:40pm    
"but the result is wrong"

And we are expected to guess what that means.
[no name] 8-Dec-16 13:43pm    
What did you find was happening in your code when you stepped through it using the debugger?

Having people finding bugs for you is way coll, but you don't learn a lot that way. By using the debugger you will learn much more, and your skills will improve.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect, inspect variables as your code execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
I think it would be simpler to start by reversing the whole line, use RegEx to locate numbers and then replace them with their reverses.
 
Share this answer
 
v2
Start by making your life easier: instead of using File.ReadAllText and working out the lines yourself, use File.ReadAllLines and you get the file broken down for you.

Then write a method which takes a String parameter, and which returns a String. This is your "line reverse" method.

Start by looking at the input string from the tail end rather than the beginning.
If the character is a letter, add it to the output. If it's number, find the first digit in the sequence and copy the number to the output in it's current form. Then set the loop to work on the character before the number started.
So
USA 123 UAE builds up in the output as
E
EA
EAU
EAU<space>
EAU 123
EAU 123<space>
EAU 123 A
EAU 123 AS
EAU 123 ASU

That isn't difficult to do, if you think about it for a moment (think about using a For loop to count down the character index and access the input string as an array of characters) but this is your homework, so I'm not coding it for you!

(If you are familiar with the StringBuilder class, that would be an excellent type to use while building up your output. If not, a String will do.)
 
Share this answer
 
Leave the coding aside first. You need a plan, everything needs a plan to start up with, almost. Devise the correct method to solving your problem, or so-called 'Algorithm'. Write it down using paper and pen. Describe the approach step by step using natural language. Sort out the logic, repeat until it makes sense. Once this is done. You get the so-called pseudo code describing the algorithm of solving your problem. Now then can you sit down and transform this pseudo code into a computer program using a any computer programming language.
For your question, the pseudo code may look like:
Step 1  Reverse the (original) string character by character.
Step 2  Scan through the (reversed) string character by character,
    2.1  When a digit is found, reversed it.

Alternatively, you may start with step 2 on the original string followed by step 1.

Notice, two of the operations involving string reversal are similar, they are candidates for re-use. Take note of this in your coding.
To improve your code, ask yourself, do you need to reverse a single digit?
I will not provide code, but your can google for relevant methods in your programming language of choice that can perform the following operations:
1. Pattern matching (Regex).
2. String manipulation.
3. Looping construct.
Enjoy learning...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900