這篇文章主要介紹了C語言簡(jiǎn)單實(shí)現(xiàn)計(jì)算字符個(gè)數(shù)的方法,涉及C語言針對(duì)字符串的簡(jiǎn)單遍歷與判定技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C語言簡(jiǎn)單實(shí)現(xiàn)計(jì)算字符個(gè)數(shù)的方法。分享給大家供大家參考。具體如下:
char_counting.c如下:
- #include<stdio.h>
- int main()
- {
- long nc;
- nc = 0;
- while(getchar() != '0')
- {
- ++nc;
- }
- printf("%ld/n", nc);
- }
編譯和使用下:
復(fù)制代碼代碼如下:
gcc char_counting.c -o char_counting.o
一種通常的調(diào)用方式:
復(fù)制代碼代碼如下:
[root@server1 c]# ./char_counting.o
123
450[回車]
6
通過linux管道來傳遞字符:
復(fù)制代碼代碼如下:
[root@server1 c]# echo helloworld0 | ./char_counting.o [回車]
10
希望本文所述對(duì)大家的C語言程序設(shè)計(jì)有所幫助。