美文网首页
[习题14]编写和使用函数

[习题14]编写和使用函数

作者: AkuRinbu | 来源:发表于2018-09-19 23:01 被阅读15次

使用教材

《“笨办法” 学C语言(Learn C The Hard Way)》
https://www.jianshu.com/p/b0631208a794

ex14.c

  • 打印属于字母或者空格的字符及其对应的ASCII值
#include <stdio.h>
#include <ctype.h>
#include <string.h>

// forward declaration
void print_letters(char arg[], int len);

void print_arguments(int argc, char *argv[]) 
{
    int i = 0 ;
    int len = 0;

    for(i = 0; i < argc; i++) {
        len = strlen(argv[i]);
        print_letters(argv[i], len);
    }
}

void print_letters(char arg[], int len) 
{
    int i = 0 ;

    for(i = 0 ; i< len; i++) {
        char ch = arg[i];

        if(isalpha(ch) || isblank(ch)) {
            printf("'%c' == %d \n", ch, ch);
        }
    }

    printf("\n");
}


int main(int argc, char *agrv[]) 
{
    print_arguments(argc, agrv);
    return 0;
}

Makefile

CC=clang
CFLAGS=-Wall -g

clean:
    rm -f ex14

run

anno@anno-m:~/Documents/mycode/ex14$ make ex14
clang -Wall -g    ex14.c   -o ex14
anno@anno-m:~/Documents/mycode/ex14$ ./ex14
'e' == 101 
'x' == 120 

anno@anno-m:~/Documents/mycode/ex14$ ./ex14 "I go 3 space"
'e' == 101 
'x' == 120 

'I' == 73 
' ' == 32 
'g' == 103 
'o' == 111 
' ' == 32 
' ' == 32 
's' == 115 
'p' == 112 
'a' == 97 
'c' == 99 
'e' == 101 

anno@anno-m:~/Documents/mycode/ex14$ ./ex14 hell1o worl2d !3
./ex14 hell1o worl2d ls
'e' == 101 
'x' == 120 

'h' == 104 
'e' == 101 
'l' == 108 
'l' == 108 
'o' == 111 

'w' == 119 
'o' == 111 
'r' == 114 
'l' == 108 
'd' == 100 

'l' == 108 
's' == 115 

note

  • 前置声明 forward declaration:解决未定义函数之前就可以使用这个函数;

  • isalpha(ch)isblank(ch)来自头文件<ctype.h>

SYNOPSIS
       #include <ctype.h>

       int isalnum(int c);
       int isalpha(int c);
       int isascii(int c);
       int isblank(int c);
       int iscntrl(int c);
       int isdigit(int c);
       int isgraph(int c);
       int islower(int c);
       int isprint(int c);
       int ispunct(int c);
       int isspace(int c);
       int isupper(int c);
       int isxdigit(int c);
  • strlen来自<string.h> :返回字符串长度,不包括最后的\0
NAME
       strlen - calculate the length of a string

SYNOPSIS
       #include <string.h>

       size_t strlen(const char *s);

DESCRIPTION
       The  strlen() function calculates the length of the string s, excluding
       the terminating null byte ('\0').

RETURN VALUE
       The strlen() function returns the number of bytes in the string s.
  • Indentation style

https://en.wikipedia.org/wiki/Indentation_style

相关文章

  • [习题14]编写和使用函数

    使用教材 《“笨办法” 学C语言(Learn C The Hard Way)》https://www.jiansh...

  • 笨办法学C 练习14:编写并使用函数

    练习14:编写并使用函数 原文:Exercise 14: Writing And Using Functions ...

  • Python Day20 函数

    1,函数关键字 def 举个例子: 2,函数的参数 举个例子: 3,习题,习题来自fishc 0. 编写一个函数p...

  • JAVA Web学习(17)___11.6 定义和使用EL函数

    11.6 定义和使用EL函数 11.6.1 定义和使用函数 1.编写一个Java类,并在该类中编写公用的静态方法,...

  • python学习_day9

    函数的使用 练习题

  • 【笨办法学Python】- 习题18-26

    习题 18:命名、变量、代码、函数 2.习题 19:函数和变量 3.习题 20:函数和文件 4.习题 21:函数可...

  • 9月12日C++学习总结

    上午 科技创新论坛会 下午 练习题:编写图形类,要求所有类的成员函数都在类外定义,都要有构造函数和析构函数:圆类C...

  • 【Python爬虫】-习题18~26

    习题18 函数使用注意事项: 函数定义是以 def 开始的吗? 函数名称是以字符和下划线 _ 组成的吗? 函数名称...

  • 前端 javascript 练习题

    前端 javascript 练习题 函数 1.编写任意个数字的求和、差、积、商的函数 思路分析:首先求任意个数,因...

  • C++作业-导数

    一元连续函数求导。1)编写一个函数用于计算2)编写一个函数用于计算3)编写一个函数用于计算;(提示:使用exp函数...

网友评论

      本文标题:[习题14]编写和使用函数

      本文链接:https://www.haomeiwen.com/subject/lxiwnftx.html