Comparing a line with next of the same file

Hi all,

I want to compare first line with next line of a file containg single column. for example a file cotains:
NM_1
NM_1
NM_1
NM_2
NM_2
NM_3
NM_4
NM_5
NM_5
NM_5
NM_5
i want to get output as
1 NM_1
2 NM_1
3 NM_1
1 NM_2
2 NM_2
1 NM_3
1 NM_4
1 NM_5
2 NM_5
3 NM_5
4 NM_5
separated by tabs.

Can anybody help me to write C++ script to get the above output giving an input file. It will be well appreciated.

With Regards,
Aeolus
We call them "programs"; not "scripts". There is a difference.

Here's a suitable algorithm:

get first line
set value of x to 1
print out x, print out first line
set previous_line to value of current_line
for all remaining lines:
  put the next line into current_line
  if line == previous line
    { increment x}
  else
     { set x to 1}
  print out x, print out line
  set previous_line to value of current_line
Last edited on
Hi Moschops,

Thanks for your reply. But the algorithm is I know. But the confusion is that I do not know how to get the next line after getting the current line and how to compare them i n C++ program.

Pleas show me a part of the program to do this. As I am knew in C++.

With regards,
Aeolus.
Store the line in a temporary string . Then go on comparing as Moschops told, until you incur a new (different ) string then store the different string in your temporary string and continue.
Topic archived. No new replies allowed.