求c語言程式輸入一串字元以結尾並統

2021-05-02 20:11:17 字數 5914 閱讀 6571

1樓:金色潛鳥

下面是完整程式。輸入一串 字元,可以是 大小寫字母,數字,符號(含任意個數 回車,換行,空白 等等) ,一旦拍入 #號,就輸出 數字個數,字母個數,空格 和其它

#include

int main();

if (c ==' ') n_sp++;

else if (c>='0' && c <='9') n_num++;

else if ( (c>='a' && c<='z') || (c>='a' && c<='z')) n_letter++;

else n_other++;

s[n]=c;n++;

printf("you input was:\n%s\n",s);

//for (i=0;i

return 0;}

2樓:gta小雞

#include

int main()

return 0;}

c語言題目輸入一行字元,分別統計出其中英文字母,空格,數字和其他字元的個數。

3樓:非常可愛

錯誤**:

if('a'<=nextchar<='z'||'a'<=nextchar<='z')

else if('0'<=nextchar<='9')修改後:

#include

int main()

}擴充套件資料

c++輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數。

#include

int main()

printf("%d %d %d %d\n",a,b,c,d);

return 0;}

4樓:匿名使用者

錯誤**:

1.'a'<=nextchar<='z'||'a'<=nextchar<='z';

2.'0'<=nextchar<='9'。

錯誤原因:當多個條件時,需要使用邏輯運算子。

修改後**為:

int main(void)

else if (c == ' ')

else if (c >= '0'&&c <= '9')else

}printf("字母=%d,數字=%d,空格=%d,其他

return 0;}

5樓:匿名使用者

一、問題分析:

輸入一行字母,那麼會以換行結束。所以可以存入陣列,也可以逐個輸入,遇到換行結束。

要統計各個類的個數,就要逐個判斷是哪個分類的。

由於在ascii碼中,數字,大寫字母,小寫字母分別連續,所以可以根據邊界值判斷型別。

二、演算法設計:

1、讀入字元,直到遇到換行結束。

2、對於每個字元,判斷是字母還是數字,或者空格,或者是其它字元。

3、對於每個字元判斷後,對應類別計數器自加。

4、最終輸出結果。

三、參考**:

#include

int main()

printf("%d %d %d %d\n", a,b,c,d);//輸出結果。

return 0;}

6樓:gta小雞

開始↓gets()讀一行字元存到char *s中strlen()函式求字串s長度

陣列cal[4]用來累計字母、空格、數字、特殊字元的個數for(i=0;i

輸出cal陣列各元素的值結束

7樓:匿名使用者

即學了程式設計又學了英語(沒學好……),豈不美哉?

(printf()函式能用那種方式是因版本的關係)

本程式的優點:不受到字串長度的限制,執行效率高

#include

int main (void)

++resnum;                    //attention! because of the newline (ascii: 10)!

//data output

printf ("\nthe results of data processing are as fellows.\n");

printf ("the number of letters:%8d\n"

"the number of space: %8d\n"

"the number of digits: %8d\n"

"the number of others:%8d\n",

letnum, spanum, dignum, resnum);

//the end

printf ("\nthank you for your using!");

return 0;}

8樓:匿名使用者

#include

int main()

if(e>='0' && e<='9')// 數字是'0'到'9'的字元,不是ascii值0到9

if((e>=65&&e<=90)||(e>=97&&e<=122))//用c來接受字母的個數

else //用d來接受其他字元的個數

}printf("共輸入空格%d個\n",a);

printf("共輸入數字%d個\n",b);

printf("共輸入字母%d個\n",c);

printf("共輸入其他字元%d個\n",d);

return 0;}

9樓:匿名使用者

clear

accept "請輸入一串字元:" to xstore 0 to dyw,xyw,kg,sz,qtm=len(x)

for i=1 to m

x1=substr(x,i,1)

k=asc(x1)

do case

case k=32

kg=kg+1

case k>=48 and k<=57

sz=sz+1

case k>=65 and k<=90

dyw=dyw+1

case k>=97 and k<=122xyw=xyw+1

other

qt=qt+1

endcase

endfor

?"其中空格有: "+alltrim(str(kg))+"個"

?"大寫字母有: "+alltrim(str(dyw))+"個"

?"小寫字母有: "+alltrim(str(xyw))+"個"

?"數字有: "+alltrim(str(sz))+"個"

?"其它字元有: "+alltrim(str(qt))+"個"

10樓:匿名使用者

#include

int main(void)

else if(ch==' ')

else if(ch>='0'&&ch<='9')else

}printf("字母= %d,空格= %d,數字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);

return 0;}

11樓:程式設計師的每一天

c語言經典例子之統計英文、字母、空格及數字個數

12樓:瞌睡貓然

1 while語句:

#include

int main(void)

else if(ch==' ')

else if(ch>='0'&&ch<='9')else

}printf("字母= %d,空格= %d,數字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);

return 0;

}2 ,do while語句:

#include

int main(void)

else if(ch==' ')

else if(ch>='0'&&ch<='9')else

} while((ch=getchar())!='\n')//回車鍵結束輸入,並且回車符不計入

printf("字母= %d,空格= %d,數字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);

return 0;}

13樓:聽不清啊

#include

int main()

14樓:我的小名叫仙女

|#include

#define n 100

int main()

printf("英文字母:%d\n",m);

printf("數字字元:%d\n",n);

printf("空格:%d\n",b);

printf("其他字元:%d\n",c);

return 0;}

15樓:

#include

#include

int main()

int qt=strlen(c)-zm-sz-kg;

printf("字母為%d 空格為%d 數字為%d 其它為%d\n",zm,kg,sz,qt);

return 0;

}望採納,不懂可追問.

16樓:匿名使用者

輸入一行字元=input("請輸入任意資料:")

數字個數=len(list(i for i in 輸入一行字元 if i.isdigit()==1))

中英文字母個數=len(list((i for i in 輸入一行字元 if i.isalpha()==1)))

空格個數=len(list(i for i in 輸入一行字元 if i==" "))

其他個數=len(輸入一行字元)-數字個數-中英文字母個數-空格個數

print("中有個數字,箇中英文字母,個空格個數,個其他".format(輸入一行字元,數字個數,中英文字母個數,空格個數,其他個數))

17樓:匿名使用者

#include

#include

#define a 80

main()

printf("英文字元有:

%d\n",letter);

printf("數字字元有:%d\n",digit);

printf("空格有:%d\n",space);

printf("其他字元有:%d\n",others);}

18樓:匿名使用者

非要限制輸入的大小麼?過會兒給你發個

#include

#include

void main()

printf("lowercase:%d\t uppercase:%d\t digit:

%d\t space:%d\t others:%d\n",lowercase,uppercase,digit,space,others);}

19樓:磨砂玻璃杯

第10行改為if('a'<=nextchar&&nextchar<='z'||'a'<=nextchar&&nextchar<='z')

14行改為else if('0'<=nextchar&&nextchar<='9')

不能用你那種形式來表示集合

c語言試題編寫程式求任意輸入字串的

include include int main char str 100 int i printf 請輸入一個字串 n while scanf s str eof 輸入一個或多個ctrl z 後結束 printf 該字串 s acs碼值 為 n str for i 0 i 額,執行內截圖一 容並給...

c語言,輸入字串,查詢只出現一次的字元,求高手幫忙寫,謝謝謝謝謝

include stdio.h include string.h include stdlib.h char firstnotrepeatingchar char pstring 如果這個字串為空,或者字串中的每個字元都至少出現兩次return 0 int main void include usi...

C語言字串,求大佬,C語言字串陣列的問題

在語句for j 0 s j 0 j 中,j 記錄的是串s的字元個數,結束該迴圈時,s j 恰好是 0 在接下來的for迴圈中,第一個j 後,s j 是串s的最後一個字元,而i為0,迴圈體將s 0 與s j 進行交換,第二個j 是正常遞減,以便與i 相對應,交換s i 和s j 沒那個程式之類執行不...