当前位置:网站首页>[pointer] find the length of the string

[pointer] find the length of the string

2022-07-06 14:36:00 |Light|

requirement

Make up a function , Find the length of the string .( Use a pointer to achieve )

Code

/* *  This function is used to calculate formal parameters a The length of the string pointed to  *  The calculated string length is returned as a function value  *  notes : System library functions cannot be used  */
int str_len(char *a)
{
    
    int len=0;
    for(int i=0;i<300;i++)
    {
    
        if(a[i]=='\0')
        break;
        else
        len++;
    }
    return len;
}

main function

int main()
 {
    
    char a[300];
    int n;
    gets(a);
    n=str_len(a);
    printf("%d\n",n);
    return 0;
 }   

test

Test input
Those engraved in the chair behind the love, will not like the cement on the flowers, out of no wind, lonely forest.
Output
116

原网站

版权声明
本文为[|Light|]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918541766.html