簡體   English   中英

C 程序添加單個數字、大小寫字母和其他字符

[英]C program add individual digits, lower and uppercase letters, and other characters

我試圖讓我的程序打印這些:

  • 計算每個數字 (0-9)
  • 計算每個單獨的字母(az 和 AZ)
  • 任何其他類型字符的總數

輸出應該是存儲在 (10+26+26+1=) 63 個計數器中的值 問題是:計算各種英文字母(小寫和大寫)的數量。 還要計算字符總數。

我的教授並沒有像我的助教那樣真正有幫助,因為他們從未真正涉足過這個問題; 他們只是假設我們一開始就知道 C 並且在電子郵件中沒有反應。 對所有其他人感到抱歉,因為我是這個網站的新手並試圖學習。 謝謝你的耐心。

    #include<stdio.h>
    #define MAXLINE 1000
    main()
    {
    char str [MAXLINE];
    int ndigit, nlower, nupper, nother;
    int c;
    printf("Enter any text with numbers or other characters if you like: ");
    fgets(str,MAXLINE,stdin);
    ndigit = nlower = nupper = nother = 0;

    while ((c = getchar()) != EOF)
    {
            if (c >= '0' && c <= '9')
                    ++ndigit;
            else if (c >= 'a'  && c <= 'a')
                    ++nlower;
            else if (c >= 'A' && c <= 'A')
                    ++nupper;
            else
                    ++nother;
    }
            printf("Digits : %d Lowercase Letters %d Uppercase Letters %d Other characters %d\n", ndigit, nlower, nupper, nother);
    }
}

我試過運行你的程序,你的程序有兩個錯誤。

  1. 無論您在輸入中輸入什么字符串,所有內容都將存儲在str因為fgets(str,MAXLINE,stdin)因此代碼執行只會在您按下EnterEOF后移至下一行和getchar() 。請參閱此問題fgets 有什么作用? fgets是如何工作的。現在因為 getchar 永遠不會處理您的整個輸入,而循環變得無用。

  2. 以上是主要問題,刪除fgets()將允許getchar()接受字符並且循環將起作用。另一個問題是else if (c >= 'a' && c <= 'a')和 line else if (c >= 'A' && c <= 'A') ,你錯誤地代替zZ使它們aA ,所以它不起作用。

更正這兩個你的代碼會很好。

這是你的程序。

#include<stdio.h>
#define MAXLINE 1000
main()
{
char str [MAXLINE];
int ndigit, nlower, nupper, nother;
char  c;
printf("Enter any text with numbers or other characters if you like: ");
fgets(str,MAXLINE,stdin);     //What Really Happens is whatever you are
                              //inputting is getting stored into the char array str
                              //Only after you press enter or EOF then
                              // execution move forward to getchar().
  //So you type the whole string and then it all get stored in str and then 
  //comes no output because getchar never executes.

ndigit = nlower = nupper = nother = 0;

while ((c = getchar()) != '\n')  
{
        if (c >= '0' && c <= '9')
                ++ndigit;
        else if (c >= 'a'  && c <= 'a') //Change To (c>='a' && c<='z')
                ++nlower;
        else if (c >= 'A' && c <= 'A')  //Change To (c>='A' && c<='Z')
                ++nupper;
        else
                ++nother;


}
        printf("Digits : %d Lowercase Letters %d Uppercase Letters %d Other characters %d\n", ndigit, nlower, nupper, nother);
}

更改您的代碼后成為。

#include<stdio.h>
#define MAXLINE 1000  //Redundant Line As You Are No Longer Using fgets.
int main()            //Changed to int main()
{

char str [MAXLINE];   //Redundant Line As You Are No Longer Using fgets.
int ndigit, nlower, nupper, nother;
char  c;
printf("Enter any text with numbers or other characters if you like: ");
ndigit = nlower = nupper = nother = 0;

while ((c = getchar()) != '\n')
{
        if (c >= '0' && c <= '9')
                ++ndigit;
        else if (c >= 'a'  && c <= 'z')
                ++nlower;
        else if (c >= 'A' && c <= 'Z')
                ++nupper;
        else
                ++nother;


}
        printf("Digits : %d Lowercase Letters %d Uppercase Letters %d Other characters %d\n", ndigit, nlower, nupper, nother);
}

暫無
暫無

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

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