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;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
<<<<<<< HEAD
|
||||
int main(){
|
||||
char str[100];
|
||||
char subStr[100];
|
||||
@@ -26,3 +27,43 @@ int main(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
=======
|
||||
int main() {
|
||||
char str1[200], str2[100];
|
||||
int i, j, index;
|
||||
|
||||
printf("请输入主字符串:");
|
||||
fgets(str1, sizeof(str1), stdin);
|
||||
str1[strcspn(str1, "\n")] = '\0'; // 去除换行符
|
||||
|
||||
printf("请输入要插入的字符串:");
|
||||
fgets(str2, sizeof(str2), stdin);
|
||||
str2[strcspn(str2, "\n")] = '\0'; // 去除换行符
|
||||
|
||||
printf("请输入要插入的位置:");
|
||||
scanf("%d", &index);
|
||||
|
||||
int len1 = strlen(str1);
|
||||
int len2 = strlen(str2);
|
||||
|
||||
// 检查插入位置是否有效
|
||||
if (index < 0 || index > len1) {
|
||||
printf("插入位置无效\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 移动字符以腾出空间
|
||||
for (i = len1; i >= index; i--) {
|
||||
str1[i + len2] = str1[i];
|
||||
}
|
||||
|
||||
// 插入 str2
|
||||
for (j = 0; j < len2; j++) {
|
||||
str1[index + j] = str2[j];
|
||||
}
|
||||
|
||||
printf("插入后的字符串: %s\n", str1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
>>>>>>> 1843d42e7e825deacd49772a204a39bc2f132820
|
||||
|
||||
Reference in New Issue
Block a user