(數組)計算字符串個數的方法

計算字符串個數的方法

1.while(a[i]!='\0') {count++;i++;}//只要不是字符串的結束符,count加一。

#include<stdio.h>
int main()
{
    char a[80];
    int i=0,count=0;
    gets(a);
    while(a[i]!='\0') {count++;i++;}
    printf("%d",count);

}



2.strlen函數

#include<stdio.h>
#include<string.h>
int main()
{
    char a[80];
    int count=0;
    gets(a);
    count=strlen(a);//可計算字符串個數
    printf("%d",count);

}

注意:兩代碼把空格記爲一個字符。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章