Getting started with the basics of programming exercises_5
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的更多相关文章
- Getting started with the basics of programming exercises_4
1.编写一个删除C语言程序中所有的注释语句的程序.要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套. #include<stdio.h> void rcomment(int ...
- Getting started with the basics of programming exercises_3
1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...
- 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) ...
- Getting started with the basics of programming exercises_1
1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...
- Beginning C# Programming with Unity
Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...
- C语言学习书籍推荐《Practical C++ Programming》下载
下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...
- 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? ...
- LINQ Query Expressions
https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...
- 【译】微软的Python入门教程(一)
Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...
随机推荐
- IO流2 --- File类的常用方法1 --- 技术搬运工(尚硅谷)
File类的获取功能 @Test public void test2(){ File file1 = new File("hello.txt"); File file2 = new ...
- vue项目及插件
vue项目的创建 方法1: cmd中执行 vue ui vue会创建一个socket,方便快捷 方法2: 命令行建立 vue create v-proj //创建项目名为v-proj的项目文件 > ...
- python实例 列表
#! /usr/bin/python # -*- coding: utf8 -*- #列表类似Javascript的数组,方便易用 #定义元组 word=['a','b','c','d','e','f ...
- python实例 输出你好
#打开新窗口,输入: #! /usr/bin/python # -*- coding: utf8 -*- s1=input("Input your name:") print(&q ...
- golang之Sprintf函数
- C#截取字符串的方法小结(转)
1.单个字符分隔用split截取 string str = "GT123_1"; string[] strArray = str.Split('_'); //输出:sArray[0 ...
- golang数组 排序和查找
package main import "fmt" func BubbleSort(arr *[5]int){ fmt.Println("排序前arr=",(* ...
- C++学习笔记----2.4 C++对象的内存模型
转载自:http://c.biancheng.NET/cpp/biancheng/view/2995.html点击打开链接 当对象被创建时,编译器会为每个对象分配内存空间,包括成员变量和成员函数. 直 ...
- 互联网安全的必要性:CSDN用户信息泄露案告破
本报讯 昨天,记者从北京警方获悉,历时40多天的侦查,轰动互联网的“CSDN网站用户信息泄露案”告破,涉案嫌疑人已被刑拘.由于保密措施不力,北京警方还向CSDN网站开出我国落实信息安全等级保护制度以来 ...
- 订阅 如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件
如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件 标签: idea git 插件 分类: Git 最近初学Git,而且在使用的IDE是IntelliJ IDEA,发现IDE ...