.

...
Last edited on
You could read the lines of the file into a std::string and use string::find_first_of and string::replace to do the replacements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <fstream>
#include <iostream>
#include <limits>
#include <string>

void clear_cin() // just in case someone types in more than 1 character replacement
{
  std::cin.clear();
  std::cin.ignore(std::numeric_limits<int>::max(),'\n');
}

int main()
{
  std::ifstream in("input.txt");
  std::ofstream out("output.txt");
  if(in.good() && out.good())
  {
    // get replacement characters for A, B and C
    char a_rep, b_rep, c_rep;
    std::cout << std::endl << "Enter a replacement for letter \"A\": ";
    std::cin >> a_rep; clear_cin();
    std::cout << std::endl << "Enter a replacement for letter \"B\": ";
    std::cin >> b_rep; clear_cin();
    std::cout << std::endl << "Enter a replacement for letter \"C\": ";
    std::cin >> c_rep;

    std::string line;
    while(std::getline(in,line)) // read input files line-by-line
    {
      std::string::size_type pos = line.find_first_of("aAbBcC"); // are any of the characters in the string needing replacement?
      while(pos != std::string::npos) // while we are not at the end of the current line string
      {
        switch(line.at(pos)) // we found a character that needs replacing let's figure out which one
        {
          case 'a':
          case 'A':
          {
            line.replace(pos,1,1,a_rep);
          } break;
          case 'b':
          case 'B':
          {
            line.replace(pos,1,1,b_rep);
          } break;
          case 'c':
          case 'C':
          {
            line.replace(pos,1,1,c_rep);
          } break;
        }
        pos = line.find_first_of("aAbBcC"); // find the next one
      }
      out << line << std::endl; // send the altered string to output
    }
  }
}
Last edited on
OP:

The aim of this program is to read text from a text file and then replace certain letters with others of the users choice and output the new text into a file.

Currently the program does this by reading and replacing each letter in each string one at a time. I was wondering if there was a way to make it so i could for example replace all of the 'a' characters in the file at once without making the original text file one long string?

currently the program is as follows:

#include <iostream>
#include<fstream>
#include<string>
#include<algorithm>
using namespace std;

int main ()
{
string text;
char replacementA, replacementB, replacementC;
ifstream infile;
ofstream outfile;
infile.open("/Users/*********/Documents/jhhnkefkjnlf.txt");
outfile.open("/Users/*********/Documents/Myoutfile.txt");


if (!infile)
{
cout<<"unable to open file"<<endl;
exit(1);
}
else
{
while(infile)
{
infile>>text;
cout<<"Please enter the letter you would like to replace A with:";
cin>>replacementA;
replace(text.begin(), text.end(), 'a', replacementA);
replace(text.begin(), text.end(), 'A', replacementA);
cout<<"Please enter the letter you would like to replace B with:";
cin>>replacementB;
replace(text.begin(), text.end(), 'b', replacementB);
replace(text.begin(), text.end(), 'B', replacementB);
cout<<"Please enter the letter you would like to replace C with:";
cin>>replacementC;
replace(text.begin(), text.end(), 'c', replacementC);
replace(text.begin(), text.end(), 'C', replacementC);

outfile<<text<<" ";
}

}

return 0;
}

thanks,
G
Lol. :)
Don't you just love it when people do that?
Topic archived. No new replies allowed.