#include <stdio.h> #include <string.h> void myrevstr(char s1[], char s2[]); //将字符串s2反转后存放在s1中 void myrevstr(char s1[], char s2[]) { int pos = 0; char *p = s2+strlen(s2)-1; while (p>= (1) ) { s1[pos] = *p; (2) ; pos++; } (3) ; } int main() { char s1[80]; myrevstr(s1, "HelloW"); printf("%s", s1);//输出为WolleH return 1; }
时间: 2024-01-23 18:04:01 浏览: 29
完善程序如下:
```
#include <stdio.h>
#include <string.h>
void myrevstr(char s1[], char s2[]); //将字符串s2反转后存放在s1中
void myrevstr(char s1[], char s2[]) {
int pos = 0;
char *p = s2 + strlen(s2) - 1;
while (p >= s2) {
s1[pos] = *p;
p--;
pos++;
}
s1[pos] = '\0';
}
int main() {
char s1[80];
myrevstr(s1, "HelloW");
printf("%s", s1);//输出为WolleH
return 0;
}
```
其中,需要注意的点有:
1. 在 `while` 循环中,判断条件为 `p>=s2`,即指针 `p` 没有超过字符串 `s2` 的起始地址。
2. 在循环中,先将 `p` 所指向的字符赋值给 `s1` 中的对应位置,再让指针 `p` 向前移动一位。
3. 循环结束后,需要在 `s1` 的末尾添加一个空字符 `\0`,以表示字符串的结束。
相关问题
编写串的堆式存储,并且不写#include <string.h>这句
以下是一个使用堆式存储的串的示例代码,实现了串的赋值、拼接、比较和输出等基本操作,不使用<string.h>库函数:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 字符串最大长度
typedef struct {
char *str; // 字符串指针
int len; // 字符串长度
} String;
// 初始化字符串
void initString(String *s) {
s->str = (char *)malloc(MAX_SIZE * sizeof(char)); // 分配初始空间
s->len = 0;
}
// 释放字符串空间
void freeString(String *s) {
free(s->str);
s->len = 0;
}
// 赋值操作
void assignString(String *s, char *c) {
int i, len = 0;
while (c[len] != '\0') { // 计算字符串长度
len++;
}
if (len > MAX_SIZE) {
printf("String too long!\n");
exit(1); // 报错退出
}
for (i = 0; i < len; i++) {
s->str[i] = c[i]; // 逐个字符复制
}
s->str[len] = '\0'; // 添加结束符
s->len = len;
}
// 拼接操作
void concatString(String *s, char *c) {
int i, j, len = 0;
while (c[len] != '\0') { // 计算字符串长度
len++;
}
if (s->len + len > MAX_SIZE) {
printf("String too long!\n");
exit(1); // 报错退出
}
for (i = s->len, j = 0; j < len; i++, j++) {
s->str[i] = c[j]; // 逐个字符复制
}
s->str[i] = '\0'; // 添加结束符
s->len += len;
}
// 比较操作
int cmpString(String *s1, String *s2) {
int i;
for (i = 0; i < s1->len && i < s2->len; i++) {
if (s1->str[i] < s2->str[i]) {
return -1;
} else if (s1->str[i] > s2->str[i]) {
return 1;
}
}
if (s1->len < s2->len) {
return -1;
} else if (s1->len > s2->len) {
return 1;
}
return 0;
}
// 输出操作
void printString(String *s) {
printf("%s\n", s->str);
}
int main() {
String s;
initString(&s);
assignString(&s, "hello");
concatString(&s, " world");
printString(&s);
freeString(&s);
return 0;
}
```
注意在本例中,赋值和拼接操作使用了逐个字符复制的方式,而比较操作则使用了逐个字符比较的方式,效率相对较低。在实际应用中,可以根据具体情况选择使用库函数或自己实现。
1. 编一个函数,将字符串s2连接到s1后面,不用strcat函数。(请填空)#include <stdio. h> #include <string. h> void mainO ( char s1[80], s2[40]; int i=0, j=0; gets(s1) ; gets(s2) ; /*查找定位s1的连接点(即结束符) */ 8灵静话/*将s2逐个字符复制到s1后,对应下标后移*/ /*手工对s1加结束符*/ printf( "The new string is:%s” ,s1);
#include <stdio.h>
#include <string.h>
void mystrcat(char s1[], char s2[]) {
int len1 = strlen(s1);
int len2 = strlen(s2);
int i, j;
for (i = len1, j = 0; j < len2; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
}
int main() {
char s1[80], s2[40];
printf("Enter string s1: ");
gets(s1);
printf("Enter string s2: ");
gets(s2);
mystrcat(s1, s2);
printf("The new string is: %s\n", s1);
return 0;
}