Array of Pointers C++ Program - COFPROG

Array of Pointers C++ Program

CPP Longest string and the shortest string: Array of Pointers



/* Problem Statement: Ask user how many strings will he like to input. Accordingly input all the strings in array of pointers. Find the longest string and the shortest string and print them. */
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char **str;
char temp[20];
string mistr, mastr;
int n,i,k,len[20],min,max,max_position,min_position;
cout<<"\nHow many strings do you want"<<endl;
cin>>n;
str = new char*[n];
for(i=0;i<n;i++)
{
cout<<"\nEnter a string : ";
cin>>temp;
len[i] = strlen(temp);
str[i] = new char[len[i]+1];
strcpy(str[i],temp);
}
min = len[0];
for(i=0 ; i<n ; i++)
{
if(len[i]<= min)
{
min = len[i];
min_position = i;
mistr = str[min_position];
}
}
max = len[0];
for(i=0 ; i<n ; i++)
{
if(len[i]>= max)
{
max= len[i];
max_position = i;
mastr = str[max_position];
}
}
cout<<"The largest string is : "<<mastr<<endl;
cout<<"The smallest string is : "<<mistr<<endl;
for(i=0; i<n ; i++)
{
delete str[i];
}
delete str;
return 0;
}

#COFPROG
Previous
Next Post »

BOOK OF THE DAY