#include #include

using namespace std;

void find(char *name[], int n, int *p) { int min_index = 0; for(int i=1; i<n; i++) { if(strcmp(name[i], name[min_index]) < 0) { min_index = i; } }

*p = min_index;

}

int main() { const int MAX_SIZE = 100; char *strs[MAX_SIZE];

// 读入字符串,用'###'作为结束标记
int count=0;
while(true) {
    string s;
    cin >> s;
    if(s=='###') break;

    char *cstr = new char[7]; // 保证输入的字符串长度不超过6
    strcpy(cstr, s.c_str());
    strs[count++] = cstr;
}

// 调用函数查找最小字符串
int min_index;
find(strs, count, &min_index);

// 输出结果
cout << strs[min_index] << endl;

// 释放动态分配的内存
for(int i=0; i<count; i++) {
    delete[] strs[i];
}

return 0;

}

C++ 字符串比较与查找最小字符串示例

原文地址: https://www.cveoy.top/t/topic/ojMi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录