字符串逆序pta c语言
时间: 2023-12-21 17:31:07 浏览: 142
PTA 字符串逆序C语言实现
以下是两种实现字符串逆序的例子:
1. 使用C语言实现字符串逆序
```c
#include <stdio.h>
#include <string.h>
int fun(struct stu *p, int n, char *sname) {
int i;
for (i = 0; i < n; i++) {
if (strcmp(p[i].name, sname) == 0)
return p[i].score;
}
return -1;
}
void reverseString(char *p) {
int len = strlen(p);
for (int head = 0, tail = len - 1; head < len / 2; head++, tail--) {
char tmp = p[head];
p[head] = p[tail];
p[tail] = tmp;
}
}
```
2. 使用C语言实现字符串逆序的另一种方法
```c
#include <stdio.h>
#include <string.h>
void reverseString(char *p) {
int len = 0;
for (int i = 0; p[i] != '\0'; i++) {
len++;
}
for (int head = 0, tail = len - 1; head < len / 2; head++, tail--) {
char tmp = p[head];
p[head] = p[tail];
p[tail] = tmp;
}
}
```
阅读全文