Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从字符串中删除重复字符的C程序_C - Fatal编程技术网

从字符串中删除重复字符的C程序

从字符串中删除重复字符的C程序,c,C,我遇到了一个面试问题,要求在适当的位置删除给定字符串中的重复字符。 因此,如果输入为“hi there”,则预期的输出为“hi tere”。它也被告知只考虑字母的回复和所有的。 字母是小写的。我想出了以下程序。我有一些评论要说明我的逻辑。但对于某些输入,该程序并没有像预期的那样工作。如果输入为“hii”,则工作正常,但如果输入为“hi there”,则失败。请帮忙 #include <stdio.h> int main() { char str[] = "programmi

我遇到了一个面试问题,要求在适当的位置删除给定字符串中的重复字符。 因此,如果输入为“hi there”,则预期的输出为“hi tere”。它也被告知只考虑字母的回复和所有的。 字母是小写的。我想出了以下程序。我有一些评论要说明我的逻辑。但对于某些输入,该程序并没有像预期的那样工作。如果输入为“hii”,则工作正常,但如果输入为“hi there”,则失败。请帮忙

#include <stdio.h>
int main() 
{
    char str[] = "programming is really cool"; // original string.
    char hash[26] = {0}; // hash table.
    int i,j; // loop counter.
// iterate through the input string char by char.
for(i=0,j=0;str[i];)
{
    // if the char is not hashed.
    if(!hash[str[i] - 'a'])
    {
        // hash it.
        hash[str[i] - 'a'] = 1;
        // copy the char at index i to index j.
        str[j++] = str[i++];
    }
    else
    {
        // move to next char of the original string.
        // do not increment j, so that later we can over-write the repeated char.
        i++;
    }
}

// add a null char.
str[j] = 0;

// print it.
printf("%s\n",str); // "progamin s ely c" expected.

return 0;
#包括
int main()
{
char str[]=“编程真的很酷”;//原始字符串。
字符哈希[26]={0};//哈希表。
int i,j;//循环计数器。
//逐字符遍历输入字符串。
对于(i=0,j=0;str[i];)
{
//如果字符没有散列。
if(!hash[str[i]-'a']))
{
//胡说八道。
散列[str[i]-'a']=1;
//将索引i处的字符复制到索引j。
str[j++]=str[i++];
}
其他的
{
//移动到原始字符串的下一个字符。
//不要增加j,这样以后我们可以重写重复的字符。
i++;
}
}
//添加一个空字符。
str[j]=0;
//打印出来。
printf(“%s\n”,str);//“progamin s ely c”应为空。
返回0;

}

str[i]
是非字母表时,请说出空格,然后执行以下操作:

hash[str[i] - 'a']
你的程序可能会失败

space
的ASCII值为
32
a
的ASCII值为
97
,因此您可以使用负索引有效地访问数组哈希

要解决此问题,您可以通过执行以下操作忽略非字母:

if(! isalpha(str[i]) {
    str[j++] = str[i++]; // copy the char.
    continue;  // ignore rest of the loop.
}

这将中断任何空格字符(或“a”..“z”范围之外的任何字符),因为您正在访问超出哈希数组边界的内容。

// iterate through the input string char by char.
for(i=0,j=0;str[i];)
{
  if (str[i] == ' ')
  {
    str[j++] = str[i++];
    continue;
  }

    // if the char is not hashed.
    if(!hash[str[i] - 'a'])
    {

这是代码高尔夫,对吗

d(s){char*i=s,*o=s;for(;*i;++i)!memchr(s,*i,o-s)?*o++=*i:0;*o=0;}
#包括
#包括
int散列[26]={0};
有效范围内的静态整数(字符c);
静态int get_hash_码(charc);
静态字符*
删除重复的字符(字符*s)
{
尺寸长度=标准长度;
尺寸i,j=0;
对于(i=0;ireturn(c>='a'&&c您是否在预期输出中输入了一个错误;是否应该是“hi there”->“hi tere”或“hi there”->“hi ther”?其中一个函数是一个关联数组,但绝对不是哈希表。如果存在空格以外的非字母表,比如“?”,这将再次失败。您可能还需要对小写进行显式检查?两件事:in_valid_range()只是islower(),因此为了简洁起见可以删除;get_hash_code()(以及哈希代码的所有处理方式)都应该是无符号的,因为'char'可能不是,因此(int)(c-'a')可以是负数。您应该将其发布到IOCCC…;)
#include <stdio.h>
#include <string.h>

int hash[26] = {0};

static int in_valid_range (char c);
static int get_hash_code (char c);

static char * 
remove_repeated_char (char *s)
{
  size_t len = strlen (s);
  size_t i, j = 0;
  for (i = 0; i < len; ++i)
    {
      if (in_valid_range (s[i]))
    {
      int h = get_hash_code (s[i]);
      if (!hash[h])
        {
          s[j++] = s[i];
          hash[h] = 1;
        }
    }
      else
    {
      s[j++] = s[i];
    }
    }
  s[j] = 0;
  return s;
}

int
main (int argc, char **argv)
{
  printf ("%s\n", remove_repeated_char (argv[1]));
  return 0;
}

static int 
in_valid_range (char c)
{
  return (c >= 'a' && c <= 'z');
}

static int 
get_hash_code (char c)
{
  return (int) (c - 'a');
}
char *s;
int i = 0;

for (i = 0; s[i]; i++)
{
    int j;
    int gap = 0;
    for (j = i + 1; s[j]; j++)
    {
        if (gap > 0)
            s[j] = s[j + gap];
        if (!s[j])
            break;
        while (s[i] == s[j])
        {
            s[j] = s[j + gap + 1];
            gap++;
        }
    }
}
void striprepeatedchars(char *str)
{
    int seen[UCHAR_MAX + 1];
    char *c, *n;

    memset(seen, 0, sizeof(seen));

    c = n = str;
    while (*n != '\0') {
        if (!isalpha(*n) || !seen[(unsigned char) *n]) {
            *c = *n;
            seen[(unsigned char) *n]++;
            c++;
        }
        n++;
    }
    *c = '\0';
}