从0开始<五>:字符串相关-htoi函数,删除字符串中指定字符


程序一:编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀 0x或 0X)转换为与之等价的整形值。字符串中允许包含数字包括:0-9 ,a-f,A-F

解法一:(放宽了规则),这个是答案,好长呀,但可以看出来别人程序的测试还是很好的

#include <stdio.h>
#include <stdlib.h>

/* Here's a helper function to get me around the problem of not
 * having strchr
 */

int hexalpha_to_int(int c)
{
  char hexalpha[] = "aAbBcCdDeEfF";
  int i;
  int answer = 0;

  for(i = 0; answer == 0 && hexalpha[i] != '\0'; i++)
  {
    if(hexalpha[i] == c)
    {
      answer = 10 + (i / 2);
    }
  }

  return answer;
}

unsigned int htoi(const char s[])
{
  unsigned int answer = 0;
  int i = 0;
  int valid = 1;
  int hexit;

  if(s[i] == '0')
  {
    ++i;
    if(s[i] == 'x' || s[i] == 'X')
    {
      ++i;
    }
  }

  while(valid && s[i] != '\0')
  {
    answer = answer * 16;
    if(s[i] >= '0' && s[i] <= '9')
    {
      answer = answer + (s[i] - '0');
    }
    else
    {
      hexit = hexalpha_to_int(s[i]);
      if(hexit == 0)
      {
        valid = 0;
      }
      else
      {
        answer = answer + hexit;
      }
    }

    ++i;
  }

  if(!valid)
  {
    answer = 0;
  }

  return answer;
}
解法二:自己的解法,并没有什么判断,但还是很易读的,

int htoi(char s[])
{
	int i,j,ret;
	if (s[0]=='0' && (s[1]=='X' || s[1]=='x'))
		j = 2;
	else j = 0;
	ret = 0;
	for (i=j; s[i]!='\0'; ++i)	
	{
		if (s[i]>='0' && s[i]<='9')
			ret = ret*16 + s[i] - '0';
		else if (s[i]>='a' && s[i]<='z')
			ret = ret*16 + s[i] - 'a' + 10;
		else if (s[i]>='A' && s[i]<='Z')
			ret = ret*16 + s[i] - 'A' + 10;
	}
	
	return ret;
}

程序二:删除字符串s中出现的字符c

void squeeze(char s1[], char c)
{
	int i,j;
	
	for (i=j=0; s1[i]!='\0'; ++i)
	{
		if (s[i] != 'c')
			s1[j++] = s1[i];
	}
	s1[j] = '\0';
}

程序三:重新编写函数queeze,将字符串s1中任何在字符串s2中字符串匹配的字符都删除

解法:思路很简单,可以看下测试的数据,以及测试程序

void squeeze2(char s1[], char s2[])
{
  int i, j, k;
  int instr2 = 0;

  for(i = j = 0; s1[i] != '\0'; i++)
  {
    instr2 = 0;
    for(k = 0; s2[k] != '\0' && !instr2; k++)
    {
      if(s2[k] == s1[i])
      {
        instr2 = 1;
      }
    } 

    if(!instr2)
    {
      s1[j++] = s1[i];
    } 
  }
  s1[j] = '\0';
}

/* test driver */

#include <stdio.h>
#include <string.h>

int main(void)
{
  char *leftstr[] =
  {
    "",
    "a",
    "antidisestablishmentarianism",
    "beautifications",
    "characteristically",
    "deterministically",
    "electroencephalography",
    "familiarisation",
    "gastrointestinal",
    "heterogeneousness",
    "incomprehensibility",
    "justifications",
    "knowledgeable",
    "lexicographically",
    "microarchitectures",
    "nondeterministically",
    "organizationally",
    "phenomenologically",
    "quantifications",
    "representationally",
    "straightforwardness",
    "telecommunications",
    "uncontrollability",
    "vulnerabilities",
    "wholeheartedly",
    "xylophonically", /* if there is such a word :-) */
    "youthfulness",
    "zoologically"
  };
  char *rightstr[] =
  {
    "",
    "a",
    "the",
    "quick",
    "brown",
    "dog",
    "jumps",
    "over",
    "lazy",
    "fox",
    "get",
    "rid",
    "of",
    "windows",
    "and",
    "install",
    "linux"
  };

  char buffer[32];
  size_t numlefts = sizeof(leftstr) / sizeof(leftstr[0]);
  size_t numrights = sizeof(rightstr) / sizeof(rightstr[0]);
  size_t left = 0;
  size_t right = 0;

  for(left = 0; left < numlefts; left++)
  {
    for(right = 0; right < numrights; right++)
    {
      strcpy(buffer, leftstr[left]);

      squeeze2(buffer, rightstr[right]);

      printf("[%s] - [%s] = [%s]\n", leftstr[left], rightstr[right], buffer);
    }
  }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值