2

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

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

这也不是 K&R 的第 2 版

4

2 回答 2

1
/*
 * 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;
}
于 2013-03-11T03:14:47.613 回答
0

如果您已阅读 K&R 的第 1 章,其中包含一个简单的 getchar()/putchar() 组合和一个 while 循环。用于获取和显示字符,相信你会发现这个程序很熟悉。

#include<stdio.h>

int main()
{

int ch; 
    while( (ch = getchar()) != EOF)
    {
        if((ch>=65)&&(ch<=122))
        {
          if((ch>=97)&&(ch<=122))
              ch=ch-32;
          else if((ch>=65)&&(ch<=90))
          ch=ch+32;
        }
         putchar(ch);
    }
return 0;
}
于 2013-03-11T03:31:23.240 回答