您的位置:首页 > 其它

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

2015-10-17 21:05 344 查看
程序一:编写函数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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: