c - C 编程 K&R 练习 1-13

标签 c

我需要你们的帮助,我卡在 K&R 练习 1-13 了。关于功能!我几乎读完了所有 1 章,但坚持使用函数。我不明白如何使用函数。好吧,我知道如何做简单的功能,但是当我遇到更复杂的功能时,我就坚持下去了!不知道怎么传值,K&R的幂函数例子有点难懂。但不管怎样,如果你能完成练习 1 - 13,我需要你的帮助,这样我才能阅读代码并理解如何使用函数。
这里自己练习一下:
编写一个程序将其输入转换为小写,使用函数lower(c)如果c不是字母则返回c,如果是字母则返回c的小写值

如果您知道一些链接或任何关于如何处理更困难的函数(不是像将字符串传递给 main,而是算术函数)的有用信息,您能否链接它们。

另外这不是 K&R 的第 2 版

最佳答案

/*
 * A function that takes a charachter by value . It checks the ASCII value of the charchter
 * . It manipulates the ASCII values only when the passed charachter is upper case . 
 * For detail of ASCII values see here -> http://www.asciitable.com/
 */
char lower(char ch){

    if(ch >= 65 && ch <=90)
    {    ch=ch+32;
    }
    return ch;


}
int main(int argc, char** argv) {
    char str[50];
    int i,l;
    printf("Enter the string to covert ");
    scanf("%s",str);
    /*
     Get the length of the string that the user inputs 
     */
    l=strlen(str);

    /* 
     * Loop over every characters in the string . Send it to a function called
     * lower . The function takes each character  by value . 
     */
    for(i=0;i<l;i++)
    str[i]=lower(str[i]);

    /*
     * Print the new string 
     */

    printf("The changes string is %s",str);
    return 0;
}

关于c - C 编程 K&R 练习 1-13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15330406/

相关文章:

c - 将作业放入前台 shell 实现的 c

c - 在 C 中使用 C++ 可能性

c - getopt 如何设置默认值

c - bonjour 如何发现网络上的设备?

c - 指针地址符合标准的转换

c - 在C中将两个字符串合并在一起,关闭字符

c - 链表添加新的根元素

c - %r 在内核 printf 格式中意味着什么?

c - 如何告诉 GCC 为实模式生成 16 位代码

c - malloc(0) 真的有效吗?