簡體   English   中英

C編程K&R練習1-13

[英]The C Programming K&R Exercise 1-13

我需要您的幫助,我被困在K&R練習1-13中。 關於功能! 我讀了幾乎所有的第一章,但都堅持使用函數。 我不明白如何使用函數。 好吧,我知道如何做簡單的功能,但是當我遇到更復雜的功能時,我就被它困住了! 不知道如何傳遞值,冪函數的K&R示例有點難以理解。 但是無論如何,如果可能的話,我需要練習1-13的幫助,以便我閱讀代碼並了解如何使用函數。
在這里自我鍛煉:
編寫程序以將其輸入轉換為小寫,使用一個函數lower(c)如果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;
}

如果您已經閱讀了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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM