Thank you for your help.
strtok() works now as I will, although the program is not ready yet.
Here the code.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_string(char *string, int number);
int main(void)
{
char *pos1, *pos2;
char *str1, *str2;
char string[256];
int counter = 0;
int i;
printf("Enter a string: ");
get_string(string, 255);
pos1 = string;
char copy[strlen(string)+1];
while (pos1) {
str1 = malloc(strlen(string)+1);
strcpy(str1, string);
pos1 = strtok( str1, " ,");
i = 0;
while (i < counter) {
pos1 = strtok( NULL, " ,");
i++;
}
counter++;
if(pos1) {
str2 = malloc(strlen(string)+1);
strcpy(str2, string);
pos2 = strtok( str2, " ,");
while (pos2) {
if (!strcmp(pos1, pos2)) {
if ((pos1-str1)>(pos2-str2))
puts(pos1);
}
pos2 = strtok( NULL, " ,");
}
free(str2);
}
free(str1);
}
return 0;
}
void get_string(char *string, int number)
{
char ch;
char *ptr;
ptr = string;
ch = getchar();
while (ch!='\n' && (ptr-string)<(number-1)) {
*ptr = ch;
++ptr;
ch = getchar();
}
*ptr = '\0';
}