1. ## Counting unique lines/strings?

Hi,

Is there a way to count unique lines? So for example if I do this:

./count
one
two
one
one
two
three
/press ctrl + D/

the program will tell me that there are 3 unique lines and 6 lines in total.

I'm using fgets read line by line, and I have no problem getting the total number of lines, but I don't know how to approach the "unique lines" part. This is my first month of learning C, so please keep replies simple hopefully my English makes sense, and thank you so much! 2. Yes, you can keep track of the lines previously read. 3. Originally Posted by laserlight Yes, you can keep track of the lines previously read.
How can I do that? This is my code right now:
Code:
```#include <stdio.h>
#include <stdlib.h>

#define MAX_LINE 1024
#define MAX_CHARACTER 128

int main(void) {
int count, lineCount, distinctCount;
char input[MAX_LINE][MAX_CHARACTER];
// char ch[MAX_LINE][MAX_CHARACTER];

count = 0;
lineCount = 0;
distinctCount = 0;
// ch = input;
while ((fgets(input[count], MAX_CHARACTER, stdin)) != NULL) {
if (input[count] == input[count + 1]) {
distinctCount = distinctCount + 1;
}
lineCount = lineCount + 1;
count = count + 1;
}```
I tried making another array ch to store the previous line, but I don't know how to implement it within the fgets while loop?

Is there another way to approach this? I'm so lost... 4. You need to check the current line against all previous lines. You'll need a loop to go through all lines up to (but not including) the current line.

Strings are compared with strcmp, not ==.
Code:
```if (strcmp(input[count], input[i]) == 0)
// strings are equal```
In C we say x++ (or ++x) instead of x = x + 1.

lineCount is exactly the same as count. I.e., it's not needed.

distinctCount seems to be meant to count duplicates, so its name is misleading. 5. It looks like you're already keeping track of the input, by storing it in the "input" array.

I recommend you separate the code for reading input, and for checking of unique lines. In other words, first read all of the input (using your loop). When input is complete, then move on to processing the strings.

Note you cannot compare strings with == in C, you need to use "strcmp". 6. thank you so much! I was messing around with my code, should've been != and not ==. I was going to use lineCount to count the total number of lines, is there a smarter way of doing it? 7. Originally Posted by kuroholic I was messing around with my code, should've been != and not ==.
If you're referring to the string comparison, re-read algorism's and my previous responses. Originally Posted by kuroholic I was going to use lineCount to count the total number of lines, is there a smarter way of doing it?
As algorism said, you already have a variable that keeps track of the line count, so the variable "lineCount" is redundant. 8. ok I wrote something, but it still doesn't work

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 1024
#define MAX_CHARACTER 128

int main(void) {
int i, j, lineCount, distinct;
char input[MAX_LINE][MAX_CHARACTER];
char duplicate[MAX_LINE][MAX_CHARACTER];

i = 0;
lineCount = 0;
while ((fgets(input[i], MAX_CHARACTER, stdin)) != NULL) {
strncpy(duplicate[i], input[i], MAX_LINE);
lineCount = lineCount + 1;
i = i + 1;
}

i = 1;
distinct = 0;
while (i < lineCount) {
j = 0;
while (j < i) {
if (strcmp(input[i], duplicate[j]) != 0) {
//             distinct = distinct + 1;
j = j + 1;
} else {
break;
}
}
i = i + 1;
}

//    printf("%d distinct lines seen after %d lines read.\n", distinct, lineCount);

return 0;
}```
so now I'm comparing it to all the previous lines, but I don't know how to skip over strings in input that already appeared before. Popular pages Recent additions line, lines, part, total, unique 