0

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.

Vinicius
  • 55
  • 9
  • 1
    If you've already gotten a sorted list of words, all you have to do is go through it and ignore or remove any words that are the same as the previous one. – Dmitri May 07 '17 at 18:27
  • Dmtri, but I do that inside a "for"? I have no idea – Vinicius May 07 '17 at 18:28
  • If the word isn't the first one (`i` is non-zero), check if it's the same as the previous (`strcmp(words[i], words[i-1])` returns 0) and if so, ignore it. – Dmitri May 07 '17 at 18:32
  • Yeah.. still don't get where to put things, do you recommend any book or website to "understand algorithms"? – Vinicius May 07 '17 at 18:49
  • 'but I do that inside a "for" what? If you don't know that, how did you write the rest of it? – ThingyWotsit May 07 '17 at 18:49
  • Thingy, I got help from a friend that is in the same class as I, he tried to explain to me but I don't quite get it. – Vinicius May 07 '17 at 18:53

1 Answers1

0

Your code should be as follows :

#include <stdio.h>
#include <string.h>
void main()
{
  int i, j = 0, k, l, n, space = 0,pos;
  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]);

  for (i = 0;i < space;i++) //loop for removing duplicate word
  {
      if ((strcmp(words[i], words[i+1]) == 0))  // finding duplicate word
      {
        pos=i+1; //Store the location of duplicate word
        for (j = pos;j < space;j++) //loop for deleting duplicate
          {
        strcpy(words[j], words[j+1]);
      }
         space--;
         i--;
    }
  }
  printf("\n After deleting duplicate string is \n");
  for (i = 0;i <= space;i++)
    printf("%s ", words[i]);  

  return 0;
}

This will remove any duplicate words in your input string.

Mathews Sunny
  • 1,796
  • 7
  • 21
  • 31