Problem

Hi, I wanted to ask a very specific question. For my code I need to basically read 3 letters that are in alphabetic order from a txt file. For example it can be something like this DEF. But there are some letter combinations that have an error in them, like ABB, which should be ABC. What I need to do is correct the wrong ones. Does anyone have an idea of what option I could use for this ? Any help would be greatly appreciated !

Take your groups of letters in threes from the original string. You can either use the string class member function substr or simply note three consecutive indices in the string.

Compare your triplets with each of the comparators "ABC", "DEF", "GHI" etc and count the number of letters that match in the same places. If you find 3, then fine, no errors and you can move on to the next triplet. If you find 2 that match then replace with this comparator and move on to the next triplet.

I assume that everything is in upper case.

Now over to you to write some code.
One option would be to use getline to read the text from the file. Then you loop from the first to the second-last and check if the next char is greater than the previous. If not then set the next char to current char + 1.
1
2
3
4
5
6
7
8
9
10
11
12
string process(const string& s)
{
  string retval(s);

  for (size_t i = 0; i < retval.size() - 1; i++)
  {
    if (!(retval[i+1] > retval[i]))
      retval[i+1] = retval[i]+1;
  }
  return retval;
}
I think you need to re-post your entire problem @Nangs, including the test examples. Your posted (and then removed) a rather more extensive description, which would definitely affect the answers that you are given here.
Last edited on
I posted just a part of the problem since I only wanted a hint and move on from there, but the full problem is that I have 3 lines of triplet code in a txt, it looks like this:

ABCABCDEFDEFGHIGHIGHI
ABBDEFNNOPRRSTUVVXSTU
VWYSTXPPRMMOKKLGHHDBF

So basically as you can see some of them are not correct, like "ABB" or "NNO", which should be "ABC" and "MNO". What I have to do is correct the whole line of these triplets. What I'm struggling with is whether I should use string and break the whole line into triplets or use char and count out 3 letters everytime.
Last edited on
What I'm struggling with is whether I should use string and break the whole line into triplets or use char and count out 3 letters everytime.


You can do it either way, but my (personal) view is that it is easier to use string than null-terminated char arrays.

Take one line of text, say the second one
ABBDEFNNOPRRSTUVVXSTU

and consider it in groups of three (there are various ways of doing that: I'm not going to direct you to any particular one):
ABB   DEF   NNO   PRR   STU   VVX   STU

Your best matching numbers and their comparators will be
ABC(2)   DEF(3)   MNO(2)   PQR(2)   STU(3)   VWX(2)   STU(3)

so you would reassemble the string (again various ways, including just character by character) to get
ABCDEFMNOPQRSTUVWXSTU(3)


There's some ambiguity in your problem: maybe because you haven't given all of it. For example,
- what if the line of text has length other than a multiple of three?
- is case important?
Last edited on
Yes, everything is upper case. And the line of text is always going to be 7 groups of characters in threes.
Are your allowed comparators any successive set of letters, e.g. ABC, BCD, CDE, DEF,
or do they have to be the non-overlapping ones, ABC, DEF, etc. ?
They have to be non-overlapping ones as you mentioned. Basically from ABC to VWX in groups of threes.
Last edited on
So, take your string 3 elements at a time. Compare them with "ABC", "DEF", "GHI" etc. in turn (best if those are in a string array and you use a loop).

For the comparisons:
- If all three characters are the same, that's fine - this triplet is OK, so no more comparisons necessary.
- If two characters are the same, replace them with this comparator and, again move on as this triplet is now done.

Now you have to write some code.
I think you got me on track, thank you ! I'll try to to finally deal with this code.
Hey nangs - I think we are in the same class! I’m having such a hard time with the same type of problem.
Haha, it's been a pain for me aswell, have been struggling for good two weeks with this one. Think I'm about to ace it though! :)
One last question, I just started learning string, so what's a good way to compare it's characters ? Right now I'm just using simple i, i+1 and i+2 in a loop comparing for all the triplets, but this leads to a lot of what seems to be unnecesarry code. Is there a more efficient way ?
void Errors(string A[]) {
	ifstream fd(CDfd);
	int n;
	string a,b,c,d,e,f,g;
	string a1,a2,a3;
	int k;
	fd>>n;
	fd.ignore(80, '\n');
	for (int i=0; i<n; i++) {
		getline(fd, a1);
		a=a1.substr(0,3); 
		b=a1.substr(3,3); 
		c=a1.substr(6,3); 
		d=a1.substr(9,3); 
		e=a1.substr(12,3); 
		f=a1.substr(15,3);
		g=a1.substr(18,3);
		
		for (int j=0; j<3; j++) {
		 
		if ((a1[i]==a[i])&&(a1[i+1]==a[i+1]))||((a1[i+1]==a[i+1])&&(a1[i+2]==a[i+2]))||((a1[i+2]==a[i+2])&&(a1[i]==a[i]))
		
		
		}
				
		}
		
	}


This is what I have so far
Use line.substr(i,3) to get a triplet from your main string (I called it line) and compare it with your comparators. You can increase i by 3 at a time using (e.g.) a for loop with update i+=3;

Do not use variable names like a, b, c, d, e, f, g - get used to using array indices and loops.

You can also do your comparison with a loop through elements 0-2. I'm not even going to look at a mess of || and && combinations. I think you should count the matches. If you wanted to do a gross comparison of strings you can just use == for whole strings (a big advantage over char[]).

I suggest that you learn how to use string before you try coding with it. There is a very good reference on this website if you need its member functions and some example code.

When you write your program do not write it all at once and present us with a wall of code full of multiple errors. Write it in parts and get each working before you move on. I suggest something like:
(i) read your full lines from file and print them out;
(ii) break each line into triplets using a loop;
(iii) send each triplet to another function to compare with your array of comparators;
(iv) reassemble your amended string.

I repeat: do not try to write the whole thing at once without checking, as each error will have a knock-on effect on the next part and it becomes difficult to identify the primary cause of a bug.

Use cout lines frequently whilst debugging.
Last edited on
Got it. Excuse my unprofessionalism... Just learning and taking baby steps.
Topic archived. No new replies allowed.