C 練習(xí)實例70

C 語言經(jīng)典100例 C 語言經(jīng)典100例

題目:寫一個函數(shù),求一個字符串的長度,在main函數(shù)中輸入字符串,并輸出其長度。

程序分析:無。

程序源代碼:

//  Created by www.o2fo.com on 15/11/9.
//  Copyright © 2015年 W3Cschool教程. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int len;
    char str[20];
    printf("請輸入字符串:\n");
    scanf("%s",str);
    len=length(str);
    printf("字符串有 %d 個字符。",len);
}
//求字符串長度  
int length(char *s)  
{  
    int i=0;
    while(*s!='\0')
    {  
        i++;   
        s++;  
    }  
    return i;  
}  

以上程序執(zhí)行輸出結(jié)果為:

請輸入字符串:
www.o2fo.com
字符串有 14 個字符。

C 語言經(jīng)典100例 C 語言經(jīng)典100例