C Exercise Example 70
100 cases of classic C language
Title: Write a function, find the length of a string, enter the string in the main function, and outputs its length.
Program Analysis: None.
Source Code:
// Created by www.w3big.com on 15/11/9. // Copyright © 2015年 本教程. 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; }
The above program execution output is:
请输入字符串: www.w3big.com 字符串有 14 个字符。