2

K&R演習1-13で立ち往生しているあなたの助けが必要です。その機能について!私はほぼすべての1章を通り抜けましたが、関数に固執しました。関数の操作方法がわかりません。単純な関数の実行方法は知っていますが、より複雑な関数になると、それに固執します。値を渡す方法がわからない、べき関数のK&Rの例は少し理解しにくいです。しかし、とにかく、コードを読んで関数の操作方法を理解できるように、可能であれば演習1〜13であなたの助けが必要です。
ここで自分で練習し
ます。関数を使用して、入力を小文字に変換するプログラムを作成します。lower(c)witchは、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 に答える