1、编写函数,把由十六进制数字组成的字符串转换为对应的整型值

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

#define YES 1
#define NO 0 /* htoi: convert hexadecimal string s to integer */
int htoi(char s[])
{
int hexdigit, i, inhex, n; i = 0;
if(s[i] == '0') // skip optional 0x or 0X
{
++i;
if(s[i] == 'x' || s[i] == 'X')
++i;
} n = 0; // integer value to be returned
inhex = YES; // assume valid hexadecimal digit
for( ; inhex == YES; ++i)
{
if(s[i] >= '0' && s[i] <= '9')
hexdigit = s[i] - '0';
else if(s[i] >= 'a' && s[i] <= 'f')
hexdigit = s[i] - 'a' + 10;
else if(s[i] >= 'A' && s[i] <= 'F')
hexdigit = s[i] - 'A' + 10;
else
inhex = NO; // not a valid hexadecimal digit
if(inhex == YES)
n = 16 * n + hexdigit;
} return n;
}

2、编写函数,将字符串s1中任何与字符串s2中字符匹配的字符都删除

/* squeeze: delete each char in s1 which is in s2 */
void squeeze(char s1[], char s2[])
{
int i, j, k; for(i = k = 0; s1[i] != '\0'; i++)
{
for(j = 0; s2[j] != '\0' && s2[j] != s1[i]; j++)
;
if(s2[j] == '\0') // end of string - no match
s1[k++] = s1[i];
}
s1[k] = '\0';
}

3、编写函数, 将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回

编写函数any(s1, s2), 将字符串s2中的任一字符在字符串s1中第一次出现的位置作为结果返回。如果s1中不包含s2中的字符,则返回-1。(标准库函数strpbrk具有同样的功能,但它返回的是指向该位置的指针。)

/* any: return first location in s1 where any char from s2 occurs */
int any(char s1[], char s2[])
{
int i, j; for(i = 0; s1[i] != '\0'; i++)
for(j = 0; s2[j] != '\0'; j++)
if(s1[i] == s2[j]) // match found?
return i; // location first match
return -1; // otherwise, no match
}

Getting started with the basics of programming exercises_5的更多相关文章

  1. Getting started with the basics of programming exercises_4

    1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...

  2. Getting started with the basics of programming exercises_3

    1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...

  3. Getting started with the basics of programming exercises_2

    1.编写简单power函数 #include<stdio.h> int power(int m, int n); // test power function int main(void) ...

  4. Getting started with the basics of programming exercises_1

    1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...

  5. Beginning C# Programming with Unity

    Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...

  6. C语言学习书籍推荐《Practical C++ Programming》下载

    下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...

  7. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  8. LINQ Query Expressions

    https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...

  9. 【译】微软的Python入门教程(一)

    Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...

随机推荐

  1. hbase phoenix char may not be null

    在使用phoenix做hbase的相关測试的时候.会出现 char may not be null 的错误. 这是因为建表和导入的数据不匹配导致的.主要是char的定义,假如一个字段定义为char类型 ...

  2. python基础--数据类型的常用方法1

    1.数字类型 整型 用途:存qq号,手机号,不带字母的身份证号... 进制转换: 二进制转十进制:10 -->  1*(2**1) + 0*(2**0) 2 八进制转十进制:  235  --& ...

  3. day36 04-Hibernate检索方式:多表连接查询

    返回的是一个List集合,这个List集合的泛型是一个Object数组.最后会拿到一个里面放Object数组的List集合. HQL内连接查询,发出SQL语句查询出来的结果集被Hibernate封装成 ...

  4. 2019阿里云开年Hi购季云通信分会场全攻略!

    2019阿里云云上Hi购季活动已经于2月25日正式开启,从已开放的活动页面来看,活动分为三个阶段: 2月25日-3月04日的活动报名阶段.3月04日-3月16日的新购满返+5折抢购阶段.3月16日-3 ...

  5. iOS 自定义 URL Scheme 完全指南

    http://www.cocoachina.com/industry/20140522/8514.html “”   阅读器 自定义URL Scheme 本文转自Migrant的博客,原文:<T ...

  6. HTTP之request请求(注册)

    HTTP之request请求 request:请求 作用:获取浏览器发送过来的数据 组成部分:请求行 请求头 请求体 操作请求行 格式: 请求方式 请求资源 协议/版本 常用方法:HttpServle ...

  7. 多云混合云之多集群统一管理:基于阿里云ACK统一纳管多个不同Kubernetes集群

    目前阿里云云原生产品家族已经支持多集群管理功能,允许使用阿里云容器服务Kubernetes(简称ACK)控制台或kubectl命令接入.统一纳管其他公有云.客户IDC自建K8s集群,集中管理部署K8s ...

  8. Spring简介+HelloWorld

    IoC Container: 这是最重要的,也是最基础的, Spring的基础.它的作用是配置和Java对象的生命周期管理. DAO, ORM, AOP, WEB: 该模块可用于将工具或框架集成到了S ...

  9. JDBC操作数据库实例

    jdbc操作数据库实例 1.jdbc创建数据库 1.1 前提条件 1.拥有创建和删除表的权限 2.数据库已经启动,且可用 1.2 jdbc创建数据库表的步骤: 导包:导入需要进行数据库编程的 JDBC ...

  10. oracle-ORA-01567错误

    删除日志4时将在线索1中保留少于两个日志文件