从0开始<五>:字符串相关-htoi函数,删除字符串中指定字符
2015-10-17 21:05
344 查看
程序一:编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀 0x或 0X)转换为与之等价的整形值。字符串中允许包含数字包括:0-9 ,a-f,A-F
解法一:(放宽了规则),这个是答案,好长呀,但可以看出来别人程序的测试还是很好的
程序二:删除字符串s中出现的字符c
程序三:重新编写函数queeze,将字符串s1中任何在字符串s2中字符串匹配的字符都删除
解法:思路很简单,可以看下测试的数据,以及测试程序
解法一:(放宽了规则),这个是答案,好长呀,但可以看出来别人程序的测试还是很好的
#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; }
相关文章推荐
- Codeforces 585C Alice, Bob, Oranges and Apples
- 实数是如何存储在计算机内部的
- hdoj--1686--Oulipo
- 服务器安装Macfee(麦咖啡)杀毒软件后可能出现的问题
- JDBC配置
- 类与对象动手动脑实践
- Chapter 4 Divide-and-Conquer
- BC第二场
- StoryBoard 关于Segue ----prepareForSegue:sender: 理解
- java网络编程之 UDP的使用
- 好用的编辑框布局控件TdxLayoutControl
- Linux 2.6内核中新的锁机制--RCU
- ThreadPoolExecutor运行原理
- 基于R语言的关联规则实现
- String 类
- iOS应用沙盒机制浅析
- 编写猜数字游戏
- <LeetCode><Easy> 100 Same Tree
- java线程池的简单使用
- Centos6.5 下编译64位 Hadoop 2.2.0