使用设计的字符串对象实现比较字符串“怀化学院”和字符串“怀化师专”的大小,规定用c语言
时间: 2023-06-01 07:03:03 浏览: 49
实现。
以下是使用C语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *str;
int length;
} String;
int string_compare(String *s1, String *s2) {
int i;
for (i = 0; i < s1->length && i < s2->length; i++) {
if (s1->str[i] < s2->str[i]) {
return -1;
} else if (s1->str[i] > s2->str[i]) {
return 1;
}
}
if (s1->length < s2->length) {
return -1;
} else if (s1->length > s2->length) {
return 1;
}
return 0;
}
int main() {
String s1 = {"怀化学院", strlen("怀化学院")};
String s2 = {"怀化师专", strlen("怀化师专")};
int result = string_compare(&s1, &s2);
if (result < 0) {
printf("字符串\"%s\"比字符串\"%s\"小\n", s1.str, s2.str);
} else if (result > 0) {
printf("字符串\"%s\"比字符串\"%s\"大\n", s1.str, s2.str);
} else {
printf("字符串\"%s\"和字符串\"%s\"相等\n", s1.str, s2.str);
}
return 0;
}
```
输出结果为:
```
字符串"怀化学院"比字符串"怀化师专"大
```
相关推荐
![](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)
![](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)