用C语言解决的简单问题 [英] simple problem in c language

查看:68
本文介绍了用C语言解决的简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给了两个输入字符串,例如

HELLO
OPEN

然后将string1(HELLO)与string2(OPEN)比较,然后从string1中删除相同的字符.

输出是

HLL

请告诉我是否有人知道.


感谢副词.

i have given two input strings like

HELLO
OPEN

then string1 (HELLO) compares with string2(OPEN) then same characters are deleted from string1.

output is

HLL

please tell me if any one knows.


Thanks in adv.

推荐答案

当我批评那里的其他三个解决方案时,我以为我会把钱塞在嘴里,自己写一个:
As I criticised three of the other solutions up there I thought I''d stick my money where my mouth was and write one of me own:
/***/

int is_capital_letter( char to_test )
{
    return to_test <= 'Z' && to_test >= 'A';
}

/***/

unsigned capital_letter_to_index( char letter )
{
    return letter - 'A';
}

/***/

void mark_letter_to_be_eliminated(
    char elimination_table[ 26 ],
    char letter_to_eliminate )
{
    elimination_table[ capital_letter_to_index( letter_to_eliminate ) ] = 1;
}

/***/

int is_letter_to_be_kept( const char elimination_table[ 26 ], char letter_to_test )
{
    return !elimination_table[ capital_letter_to_index( letter_to_test ) ];
}

/***/

void build_elimination_table(
    const char *to_eliminate,
    char elimination_table[26] )
{
    for( ; *to_eliminate; to_eliminate++ )
    {
        if( is_capital_letter( *to_eliminate ) )
        {
            mark_letter_to_be_eliminated( elimination_table, *to_eliminate );
        }
    }
}

/***/

void remove_duplicates_using_elimination_table(
    const char *source,
    const char elimination_table[26],
    char *destination,
    const char *end_of_destination )
{
    while( *source && destination < end_of_destination )
    {
        if( is_capital_letter( *source ) )
        {
            if( is_letter_to_be_kept( elimination_table, *source ) )
            {
                *destination++ = *source;
            }

            source++;
        }
    }

    *destination = 0;
}

/***/

void remove_duplicates(
    const char *source,
    const char *to_eliminate,
    char *destination,
    const char *end_of_destination )
{
    char elimination_table[26] = { 0 };

    build_elimination_table( to_eliminate, elimination_table );

    remove_duplicates_using_elimination_table(
            source,
            elimination_table,
            destination,
            end_of_destination );
}

这是通过创建一个字母表来实现的,该字母表在复制到目标字符串时会从源字符串中消除.这些功能都很安全,不会因输入无效而崩溃,它们只会复制不是大写字母的字符.如果源和目标是别名,它仍然可以工作.

与上述某些方法的复杂度为O(n * m)(其中n是消除字符串的大小而m是要复制的字符串的大小)不同,此方法的复杂度为O(n + m) .

无论如何,您都可以这样称呼它:

This lot works by creating a table of letters that are to be eliminated from the source string as it''s copied to the destination string. The functions are all pretty safe and won''t crash with invalid input they''ll just not copy characters that aren''t capital letters. And it''ll still work if the source and destination are aliased.

Unlike some of the approaches above which have a complexity of O(n*m) where n is the size of the elimination string and m is the size of the string to be copied, this approach has a complexity of O(n+m).

Anyway you''d call it something like this:

char source[] = "HELLO";
char to_eliminate[] = "OPEN";

remove_duplicates( source, to_eliminate, source, source + sizeof source );

将显示它在源和目标引用相同的内存时仍然有效.

最后一点:它被分解为许多功能,因为...
-我在读取具有多个循环或一个条件的函数时遇到麻烦
-对小功能进行单元测试更容易

which will show it still works when source and destination refer to the same memory.

Final points: it''s broken up into so many functions because...
- I have trouble reading functions with more than one loop or one conditional
- It''s easier to unit test small functions


野蛮行径,行家应该原谅(希望如此)

Brutal force approach, connoisseurs should forgive (hopefully)



void StringTransform()
{
	const int size = 100;
	char a[size], b[size], output[size];
	
	// zeroise
	memset(a, 0, size);
	memset(b, 0, size);
	memset(output, 0, size);

	// set to initial content
	strcpy(a, "HELLO");
	strcpy(b, "OPEN");

	BOOL found;
	int k = 0;

	for (int i = 0 ; i < size ; i++)
	{
		found = FALSE;
		for (int j = 0 ; j < size ; j++)
		{
			if (a[i] == b[j])
			{
				found = TRUE;
				break;
			}
		}
		// only copy from a to output if not found in b
		if (!found)
		{
			output[k++] = a[i];
		}
	}
}


尝试:

Try:

#include <stdio.h>
#include <stdlib.h>
int main()
{
  char s[] = "HELLO";
  char t[] = "OPEN";

  size_t LS = sizeof(s) / sizeof(s[0])-1;
  size_t LT = sizeof(t) / sizeof(t[0])-1;

  size_t i, j, k, deleted;

  deleted = 0;

  i = LS;
  while (i--)
  {
    for(j=0; j<LT; j++)
    {
      if (s[i] == t[j])
      {
        deleted++;
        for (k=i; k<LS-deleted-1; k++)
        {
          s[k] = s[k+1];
        }
        break;
      }
    }
  }
  s[LS-deleted] = '\0';

  printf("%s \n", s);

  return 0;
}


这篇关于用C语言解决的简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆