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. 木卯先生的笔记---Date类、DateFormat类和Calendar类

    1.Date类 1.1 简介 Date类是 java.util 包下面的类,表示特定的瞬间,精确到毫秒. 1.2 方法 1.2.1 Date() 构造方法 public Date() :分配 Date ...

  2. Boost Asio教程集合

    http://zh.highscore.de/cpp/boost/ 第七章 https://mmoaay.gitbooks.io/boost-asio-cpp-network-programming- ...

  3. SPSS20.O---软件安装

    统计要与大量的数据打交道,涉及繁杂的计算和图表绘制.现代的数据分析工作如果离开统计软件几乎是无法正常开展.在准确理解和掌握了各种统计方法原理之后,再来掌握几种统计分析软件的实际操作,是十分必要的. 常 ...

  4. Total Number of Unicorn Companies: 188

    https://www.cbinsights.com/research-unicorn-companies   Total Number of Unicorn Companies: 188 Total ...

  5. 安装 cx_Oracle

    1.下载 oracle client instant  和  sdk,  全部解压到 /opt/instantclient_11_2/ http://www.oracle.com/technetwor ...

  6. Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程

    Excel怎么增加撤销操作的次数?Excel增加可撤销次数教程 在Excel的使用中,返回上一步是经常用到的一个工具,当数据填写有误需要查看之前的内容时,一般会通过"Ctrl Z" ...

  7. [LeetCode] Evaluate Reverse Polish Notation [2]

    题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...

  8. POJ 1150 The Last Non-zero Digit 数论+容斥

    POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...

  9. 初探uni-app

    第一步:下载 第二步:安装 第三步:创建项目 第四步:项目目录 项目运行 项目效果为 打包为原生App(云端) 运行打包 使用vue 效果如下,并不好看 使用代码如下 未完,待续.... 就是对着官网 ...

  10. GBRT(GBDT)(MART)(Tree Net)(Tree link)

    源于博客 GBRT(梯度提升回归树)有好多名字,标题全是它的别名. 它是一种迭代的回归树算法,由多棵回归树组成,所有树的结论累加起来得到最终结果.在被提出之初与SVM一起被认为是泛化能力较强的算法. ...