c语言求串长度输入字符串

输入
输入在一行中给出1个长度不超过20的字符串s。

输出
在一行中输出s的串长。

输入
welcome to acm world
输出
20

供参考:

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

循环读入,直到读入换行符退出循环

int a[100],i=0;
while((a[i]=getchar())!='\n'){
  i++;
}
printf("%d",i);