在C语言中,要输出最短的字符串,首先需要定义一个函数来判断两个字符串哪个更短。然后使用printf()
函数将较短的字符串输出到控制台。以下是一个示例代码:
#include<stdio.h>
#include<string.h>
// 函数原型声明
const char* find_shortest_string(const char* str1, const char* str2);
int main() {
const char* str1 = "Hello";
const char* str2 = "World!";
const char* shortest_string = find_shortest_string(str1, str2);
printf("The shortest string is: %s\n", shortest_string);
return 0;
}
// 函数实现
const char* find_shortest_string(const char* str1, const char* str2) {
if (strlen(str1) < strlen(str2)) {
return str1;
} else {
return str2;
}
}
这个程序首先定义了一个名为find_shortest_string
的函数,该函数接收两个字符串参数,并返回较短的字符串。在main
函数中,我们调用这个函数并将结果存储在shortest_string
变量中。然后使用printf()
函数将最短的字符串输出到控制台。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c语言字符串怎么输出