C语言实现查找字符串数组中的最小字符串
#include <stdio.h> #include <string.h> #include <stdlib.h>
void find(char *name[], int n, int *p);
int main() { char str[7]; char *strings[100]; int i = 0; while (1) { scanf('%s', str); if (strcmp(str, '####') == 0) break; strings[i] = malloc(sizeof(char) * 7); // 动态分配存储空间 strcpy(strings[i], str); // 复制字符串 i++; } int index = -1; find(strings, i, &index); // 调用函数查找最小串 printf('%s\n', strings[index]); // 输出最小串 for (int j = 0; j < i; j++) { free(strings[j]); // 释放存储空间 } return 0; }
void find(char *name[], int n, int *p) { *p = 0; for (int i = 1; i < n; i++) { if (strcmp(name[i], name[*p]) < 0) { *p = i; // 找到值最小的字符串的下标 } } }
原文地址: https://www.cveoy.top/t/topic/ojLn 著作权归作者所有。请勿转载和采集!