程序,用于在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