用c++写从键盘上输入多个字符串每个串不超过6个字符且没有空格。用####作为串输入结束的标记。从所输入的若干字符中找出一个最小的串并输出该串要求小串的查找通过调用编写的函数实现void findchar nameint nintp在包含的n个字符中的二维字符数组name中查找值最小的宁符串将其下标存入指针p所指内存单元 输入一行入一个字符串输入多行输出输出个字符串单独占一行
#include
void find(char *name[], int n, int *p) { int minIndex = 0; for(int i = 1; i < n; i++) { if(strcmp(name[i], name[minIndex]) < 0) { minIndex = i; } } *p = minIndex; }
int main() { const int MAX_N = 100; // 最多输入100个字符串 char *name[MAX_N]; // 字符串指针数组 int n = 0; // 实际输入的字符串个数 char str[7]; // 用于输入字符串的字符数组 while(true) { cin >> str; if(strcmp(str, "####") == 0) { // 输入结束 break; } name[n] = new char[7]; // 动态分配内存 strcpy(name[n], str); // 将输入的字符串拷贝到动态分配的内存中 n++; } int minIndex; find(name, n, &minIndex); // 查找最小字符串 cout << name[minIndex] << endl; // 输出最小字符串 // 释放动态分配的内存 for(int i = 0; i < n; i++) { delete[] name[i]; } return 0;
原文地址: https://www.cveoy.top/t/topic/fDEy 著作权归作者所有。请勿转载和采集!