程序,用于在C ++中查找字符串中最大和最小的ASCII值字符
在本教程中,我们将讨论一个程序来查找字符串中最大和最小的ASCII值字符。
为此,我们将提供一串小写和大写字符。我们的任务是根据ASCII值找到最大和最小字符。
示例
#include <iostream>
using namespace std;
//返回最大字符
char largest_alphabet(char a[], int n) {
char max = 'A';
for (int i=0; i<n; i++)
if (a[i] > max)
max = a[i];
return max;
}
//返回最小字符
char smallest_alphabet(char a[], int n) {
char min = 'z';
for (int i=0; i<n-1; i++)
if (a[i] < min)
min = a[i];
return min;
}
int main() {
char a[]= "nhooo";
int size = sizeof(a) / sizeof(a[0]);
cout << "Largest and smallest alphabet is : ";
cout << largest_alphabet(a,size)<< " and ";
cout << smallest_alphabet(a,size)<<endl;
return 0;
}输出结果
Largest and smallest alphabet is : u and P
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短