Auto commit
This commit is contained in:
+28
-19
@@ -33,29 +33,38 @@ int main(){
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(){
|
||||
char str[100];
|
||||
char temp;
|
||||
int i, j;
|
||||
|
||||
gets(str);
|
||||
|
||||
for(i=0;i<strlen(str)-1;i++){
|
||||
for(j=0;j<strlen(str)-1-i;j++){
|
||||
if(*(str+j) > *(str+j+1)){
|
||||
temp = *(str+j);
|
||||
*(str+j) = *(str+j+1);
|
||||
*(str+j+1) = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
puts(str);
|
||||
return 0;
|
||||
// 比较函数,用于 qsort
|
||||
int compare_strings(const void *a, const void *b) {
|
||||
return strcmp(*(const char **)a, *(const char **)b);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 测试用例:字符串数组
|
||||
const char *strings[] = {
|
||||
"banana",
|
||||
"apple",
|
||||
"cherry",
|
||||
"date",
|
||||
"blueberry"
|
||||
};
|
||||
|
||||
// 计算数组的长度
|
||||
int length = sizeof(strings) / sizeof(strings[0]);
|
||||
|
||||
// 使用 qsort 对字符串数组进行排序
|
||||
qsort(strings, length, sizeof(const char *), compare_strings);
|
||||
|
||||
// 输出排序后的字符串数组
|
||||
printf("Sorted strings:\n");
|
||||
for (int i = 0; i < length; i++) {
|
||||
printf("%s\n", strings[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 字符串插入
|
||||
|
||||
+28
-19
@@ -1,24 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(){
|
||||
char str[100];
|
||||
char temp;
|
||||
int i, j;
|
||||
|
||||
gets(str);
|
||||
|
||||
for(i=0;i<strlen(str)-1;i++){
|
||||
for(j=0;j<strlen(str)-1-i;j++){
|
||||
if(*(str+j) > *(str+j+1)){
|
||||
temp = *(str+j);
|
||||
*(str+j) = *(str+j+1);
|
||||
*(str+j+1) = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
puts(str);
|
||||
return 0;
|
||||
// 比较函数,用于 qsort
|
||||
int compare_strings(const void *a, const void *b) {
|
||||
return strcmp(*(const char **)a, *(const char **)b);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 测试用例:字符串数组
|
||||
const char *strings[] = {
|
||||
"banana",
|
||||
"apple",
|
||||
"cherry",
|
||||
"date",
|
||||
"blueberry"
|
||||
};
|
||||
|
||||
// 计算数组的长度
|
||||
int length = sizeof(strings) / sizeof(strings[0]);
|
||||
|
||||
// 使用 qsort 对字符串数组进行排序
|
||||
qsort(strings, length, sizeof(const char *), compare_strings);
|
||||
|
||||
// 输出排序后的字符串数组
|
||||
printf("Sorted strings:\n");
|
||||
for (int i = 0; i < length; i++) {
|
||||
printf("%s\n", strings[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user