Click here to Skip to main content
14,734,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let me give an example real quick. So if the text file had, "The dog and cat got along. The dog and cat were friends." How could I switch the text in the file, while saving this new text to a different file, so that it would read, "The cat and dog got along. The cat and dog were friends." I just want them to switch places.

What I have tried:

I have tried to get the text file open and implemented into the code, but that is the part I'm stuck on. How do I get it so the code will automatically replace these key phrases and save the replaced text file to a different text file?
Posted
Updated 21-Nov-20 14:17pm

It's not as trivial as you may think: although in your example it's fairly simple, it gets a lot more complicated very quickly - because text files have no concept of "words" just characters. Which means that unless - as in your example - the two words to be exchanged are the same length you need to copy the file up to the beginning of a word, copy it's replacement in, skip the found word and then continue looking, copying while you go.
When complete, delete the input file and rename the output file.

The way I'd start is by reading the input and isolating the words, comparing and copying to the output file as you go. Compare each word to output before you write it and replace as necessary.
That's fairly easy to do: start by locating each word and just copying them to the output (complete with non-word characters) so you can easily compare the input and output files to make sure it works; then add the compare-and-swap logic.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
   
I think I understand your idea here, but I'm not sure exactly what to do on the coding end of things. I don't even know what code I would type in to do these things. If it's not too much trouble could I ask you to type up a little code to get me started? I'm pretty sure I understand the premise I just need the directory knowledge to be able to accomplish it. Thank you for your response so far.
OriginalGriff 21-Nov-20 3:16am
   
No, I'm not going to do your homework for you! :laugh:
Start simple, and move on from there: write code which reads the input file character by character and copies it to the output. Check it works, and then move on to "finding words" Get that working, and so on.

This isn't a complicated task, so give it a try!
   
Yah, I didn't expect you to do my homework, I am just asking how I would be able to save the text file that is being edited to a new text file while keeping the old file the same. Like what syntax would I use for that?
OriginalGriff 21-Nov-20 14:01pm
   
Well, you know how to open a file - you say that in the question - so open a second file for write access instead of read and write to it.
Quote:
How could I switch the text in the file, while saving this new text to a different file

Short answer, you don't.
Basically:
you read the input file to memory (string variable or buffer)
Then detect first position of each words, in fact, only position of next word to replace is intresting
The dog and cat got along. The dog and cat were friends.
T1  D1 T2   C1 T3              D2 T4   C2 T5

For any random text T, you write it as you encounter.
For any interesting word D and C, you write its replacement.

The fact that it is a swap prevent from replacing directly in the string.
   
I would start by tokenizing the sentence. You can use the function I posted here[^] and here[^]. Once you have it tokenized, you can rearrange the words and then print them back out.

One way you can do that is to make an array of integers that are the indexes of the tokens. Initially that array should have the values 0,1,2,3,4,5... up to the number of words (tokens) in the sentence. To swap words you just swap indexes in the array. In your first sentence there are six words , dog is word one and cat is word three so the initial array is {0,1,2,3,4,5} and swapping words one and three result in an array of {0,3,2,1,4,5}. Then you can loop through the array and write out each token as given by the array index. That means while looping on n, index=array[n] so print tokens[index].

This is not a perfect way to do it but it is one way. There will be some issues to solve but that is the nature of programming.
   

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