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. NOIP模拟赛 17.10.10

    初次见面(firstmeet)[题目背景]雾之湖边,静得可怕.露米娅出神凝望.黑白连衣裙,像极了绽放的墨黑和洁白的莲.身边的雾之湖,倒映着血色天空.酒红的双眸,映照一切.低声浅笑,双臂伸直,她悄无声息 ...

  2. 使用JSONObject进行序列化时,避开定义get或set为开头的方法名称

    从结果中可以看到,JSONObject对Test对象进行序列化时,把fileName也当做属性了. 原因:涉及到JavaBean规范(参考:https://www.cnblogs.com/yusimi ...

  3. 【Codeforces Round #430 (Div. 2) C】Ilya And The Tree

    [链接]点击打开链接 [题意] 给你一棵n个点的树,每个点的美丽值定义为根节点到这个点的路径上的所有权值的gcd. 现在,假设对于每一个点,在计算美丽值的时候,你可以将某一个点的权值置为0的话. 问你 ...

  4. 【Python之路22】冒泡排序算法

    1.变量互换 a = 123 b = 456 temp = a a = b b = temp python比较简单的变量互换: a = 123 b = 456 a,b = b,a print(a,b) ...

  5. etree不存在解决方法

    from lxml import html text=xxx//测试的html文本 etree = html.etree htmlDiv = etree.HTML(text) title = html ...

  6. [Vue CLI 3] 配置解析之 css.extract

    大家还记得我们在老版本中,对于线上环境配置中会把所有的 css 多打成一个文件: 核心是使用了插件 extract-text-webpack-plugin,方式如下: 第一步都是加载插件 const ...

  7. <第一周>降维

    PCA 矩阵的主成分就是其协方差矩阵对应的特征向量,按照对应的特征值大小进行排序,最大的特征值为第一主成分,以此类推 主要过程 对所有样本进行中心化 计算样本的协方差矩阵 XX.T 对协方差矩阵做特征 ...

  8. day38 13-Spring的Bean的属性的注入:SpEL注入

    Spring2.5提供了名称空间p注入属性的方式,Spring3.几提供了SpEL属性注入的方式. <?xml version="1.0" encoding="UT ...

  9. VisualVM介绍使用

    1    打开VisualVM(这个工具放在JDK安装目录的bin目录下,双击jvisualvm.exe即可打开),如下图所示 以VisualVM自身为例,VisualVM本身也是一个java程序,当 ...

  10. php is_null、empty、isset的区别

    isset 判断变量是否已存在 empty 判断变量是否为空或为0 is_null 判断变量是否为NULL 变量 empty is_null isset $a=”” true false true $ ...