C语言查找字符串数组中最小字符串的代码实现
#include <stdio.h> #include <string.h>
void find(char *name[], int n, int *p);
int main() { char str[10]; char *name[100]; int n = 0; while (1) { scanf('%s', str); if (strcmp(str, '####') == 0) { break; } name[n] = (char *)malloc(sizeof(char) * 7); strcpy(name[n], str); n++; } int *p = (int *)malloc(sizeof(int)); find(name, n, p); printf('%s\n', name[*p]); 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/ojLf 著作权归作者所有。请勿转载和采集!