【问题标题】:Sort and remove duplicates in a string in C在C中对字符串中的重复项进行排序和删除
【发布时间】:2017-05-07 18:19:12
【问题描述】:

我在“创建”代码方面遇到了问题,我刚开始上大学,我需要帮助解决这个问题:制作一个 C 程序,用于对字符串中的重复单词进行排序和删除。

例如:

输入:土豆土豆苹果橙草莓橙

输出:苹果橙土豆草莓

这是我目前所做的,它只是排序,但不会删除重复项:

#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;
}

我还没有学过指针或函数,也不能用它们来做这段代码。

谢谢你, 阿莫林。

【问题讨论】:

  • 如果你已经得到了一个排序的单词列表,你所要做的就是通过它并忽略或删除任何与前一个相同的单词。
  • Dmtri,但我在“for”中这样做?我不知道
  • 如果单词不是第一个(i 非零),检查它是否与前一个相同(strcmp(words[i], words[i-1]) 返回 0),如果是,则忽略它。
  • 是啊.. 还是不知道该放在哪里,有什么书或网站可以“理解算法”吗?
  • '但是我在“for”里面做了什么?如果你不知道,你是怎么写的?

标签: c algorithm sorting bubble-sort


【解决方案1】:

你的代码应该如下:

#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;
}

这将删除输入字符串中的所有重复单词。

【讨论】:

  • 我打错了,看看你现在能不能点击链接
  • 如果我改为gets会更好吗?会删除一些不必要的行
  • 我在 GCC 编译器中使用 Code::Blocks
  • check it.检查图像并验证我是否说错了
猜你喜欢
  • 2020-12-13
  • 2014-04-29
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 2013-11-21
  • 2021-12-18
相关资源
最近更新 更多