请用C语言实现:按如下函数原型编程实现字符串连接函数strcat()的功能。 void MyStrcat(char dstStr[], char srcStr[]); 输入提示信息: "Input a string:" "Input another string:" 输入字符串用gets() 输出提示信息和格式:"Concatenate results:%s\n" 程序运行示例: Input a string:abc↙ Input another string:def↙ Concatenate results:abcdef
时间: 2024-03-18 15:41:50 浏览: 21
以下是实现代码:
```c
#include <stdio.h>
#include <string.h>
void MyStrcat(char dstStr[], char srcStr[]) {
int dstLen = strlen(dstStr);
int srcLen = strlen(srcStr);
int i;
for (i = 0; i < srcLen; i++) {
dstStr[dstLen + i] = srcStr[i];
}
dstStr[dstLen + i] = '\0';
}
int main() {
char dstStr[100], srcStr[100];
printf("Input a string:");
gets(dstStr);
printf("Input another string:");
gets(srcStr);
MyStrcat(dstStr, srcStr);
printf("Concatenate results:%s\n", dstStr);
return 0;
}
```
程序运行结果如下:
```
Input a string:abc
Input another string:def
Concatenate results:abcdef
```
相关问题
按如下函数原型编程实现字符串连接函数strcat()的功能。
void strcat(char *dest, const char *src);
实现字符串连接函数strcat()的功能,可以按照以下步骤进行:
1. 定义一个指针变量p,指向dest字符串的末尾位置。
2. 通过循环遍历src字符串,将src字符串中的每个字符依次添加到dest字符串的末尾位置。
3. 在循环结束后,将dest字符串的末尾位置设置为'\',表示字符串的结束。
具体实现代码如下:
void my_strcat(char *dest, const char *src) {
char *p = dest;
while (*p) {
p++;
}
while (*src) {
*p++ = *src++;
}
*p = '\';
}
不要使用strcat函数编程实现字符串连接函数strcat的功能,将字符串srcstr连接到字符串dststr的尾部。 C语言代码
可以使用指针来实现字符串连接函数strcat的功能,具体实现如下:
```c
void my_strcat(char *dststr, char *srcstr) {
while (*dststr) {
dststr++;
}
while (*srcstr) {
*dststr++ = *srcstr++;
}
*dststr = '\0';
}
```
其中,第一个while循环用来找到dststr字符串的末尾,第二个while循环用来将srcstr字符串逐个字符复制到dststr字符串的末尾。最后,在dststr字符串的末尾添加一个空字符'\0',表示字符串的结束。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)