Here is a C++ program to find the smallest and largest character in a string
SOURCE CODE OUTPUT
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
char str[100],smchtr,lrchtr;
cout<<"Enter string : ";
cin>>str;
smchtr=lrchtr=str[0];
for(int i=1;str[i]!='\0';i++)
{
if(smchtr>str[i])
smchtr=str[i];
if(lrchtr<str[i])
lrchtr=str[i];
}
cout<<"Largest character : "<<lrchtr<<endl;
cout<<"Smallest character : "<<smchtr;
return 0;
}
#include<stdlib.h>
using namespace std;
int main()
{
char str[100],smchtr,lrchtr;
cout<<"Enter string : ";
cin>>str;
smchtr=lrchtr=str[0];
for(int i=1;str[i]!='\0';i++)
{
if(smchtr>str[i])
smchtr=str[i];
if(lrchtr<str[i])
lrchtr=str[i];
}
cout<<"Largest character : "<<lrchtr<<endl;
cout<<"Smallest character : "<<smchtr;
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment