strlen的原型及其定义

strlen()函数说明

        返回指定字符串长度,不包括结束字符'/0'

 

strlen()函数原型

        int strlen(const char *str)

 

strlen()函数定义

        int strlen(const char *str)

        {

             if (NULL == str)

                 throw"Invalid Argument!";

             int len;

             for(len=0; *str != '/0'; str++)

                 len++;

             return n;

        }

 

以下是使用C语言编写的insertString函数代码: ```c #include <stdio.h> #include <string.h> void insertString(char destinationString[], char *sourceStringPtr, int position); int main(void) { char str[100] = "Hello, world!"; char insertStr[20] = "beautiful "; int position = 7; printf("Before insert: %s\n", str); insertString(str, insertStr, position); printf("After insert: %s\n", str); return 0; } void insertString(char destinationString[], char *sourceStringPtr, int position) { int destLength = strlen(destinationString); int sourceLength = strlen(sourceStringPtr); int i; /* 移动destinationString中的字符,腾出插入位置 */ for (i = destLength; i >= position; i--) { destinationString[i + sourceLength] = destinationString[i]; } /* 插入sourceStringPtr中的字符 */ for (i = 0; i < sourceLength; i++) { destinationString[position + i] = sourceStringPtr[i]; } } ``` 程序中的insertString函数接受三个参数:目标字符串destinationString、源字符串sourceStringPtr和插入位置position。它首先计算出目标字符串和源字符串的长度,然后使用for循环将目标字符串中插入位置及其后面的字符向后移动sourceLength个位置,以便为源字符串中的字符腾出空间。接下来,使用另一个for循环将源字符串中的字符插入到目标字符串中的插入位置。最后,函数将修改后的目标字符串作为输出。 在程序运行时,主函数中定义了一个目标字符串、一个源字符串和一个插入位置,并将它们作为参数传递给insertString函数。程序输出了插入前和插入后的目标字符串,以验证插入操作是否成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值