设计一个C语言程序,先将在字符串s中的字符按正序存放到t串中,然后把s中的字符按逆序连接到t串的后面
时间: 2024-03-16 20:47:04 浏览: 75
好的,下面是我为你编写的程序,注意:程序只能处理字符数组,不能处理字符串。
```c
#include <stdio.h>
#include <string.h>
void reverse(char *str, int len) {
int i, j;
char temp;
for (i = 0, j = len - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
void reverse_concatenate(char *s, int len, char *t) {
int i, j;
for (i = 0; i < len; i++) {
t[i] = s[i];
}
reverse(s, len);
for (j = 0; j < len; j++) {
t[i + j] = s[j];
}
}
int main() {
char s[] = "hello world";
int len = strlen(s);
char t[len * 2];
reverse_concatenate(s, len, t);
printf("s = %s\nt = %s\n", s, t);
return 0;
}
```
程序的思路是,先将s中的字符正序存放到t中,然后将s中的字符反转,并连接到t的后面。reverse函数用于反转字符串,reverse_concatenate函数用于完成题目要求。在程序中,假设s字符串长度为len,t字符串长度为len*2(因为t需要存储s和s的反转)。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)