I have a problem with "creating" a code, I just started the college and I need help with this problem: make a C program where you sort and remove duplicate words in a string.
E.g.:
input: potato potato apple orange strawberry orange
output: apple orange potato strawberry
This is what I got into so far, it only sorts, but doesn't remove the duplicates:
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, k, space = 0;
char str[100], words[50][100], cmp[50];
printf("Enter the string \n");
scanf(" %[^\n]s", str);
for (i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
space++;
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
words[i][k] = '\0';
i++;
k = 0;
}else
words[i][k++] = str[j];
}
for (i = 0;i < space;i++) //loop for sorting
{
for (j = i + 1;j <= space;j++)
{
if ((strcmp(words[i], words[j]) > 0)) //swapping strings
{
strcpy(cmp, words[i]);
strcpy(words[i], words[j]);
strcpy(words[j], cmp);
}
}
}
printf("After sorting string is \n");
for (i = 0;i <= space;i++)
printf("%s ", words[i]);
return 0;
}
I still haven't learn about pointers or functions and can't use them to do this code.
Thank you, Amorim.