"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Tuesday, February 23, 2010

Write a C++ program that reads three strings and prints the longest and smallest string.


#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
char str1[30], srt2[30], str3[30];

cout<<"\n Enter 3 strings /Words one-by-one(press enter each time) :";
gets(str1);
gets(str2);
gets(str3);

/* Find Longest String */
if(strcmp(str1, str2) > 0)
{
   if(strcmp(str1,str3)>0)
       cout <<"\n The Longest string is : " << str1;
  else
       cout <<"\n The Longest string is : " << str3;
}
else
{
if(strcmp(str2,str3)>0)
cout <<"\n The Longest string is : " << str2;
else
cout <<"\n The Longest string is : " << str3;
}


/* Find smallest String */


if(strcmp(str1, str2) < 0)

{
if(strcmp(str1,str3)<0)
cout <<"\n The Smallest string is : " << str1;
else
cout <<"\n The Smallest string is : " << str3;
}
else
{
if(strcmp(str2,str3)<0)
cout <<"\n The Smallest string is : " << str2;
else
cout <<"\n The Smallest string is : " << str3;
}

getch();
}

2 comments:

  1. The program is showing an error as"cannot convert intto char constant *"

    ReplyDelete
  2. which compiler are you using?
    Include the header files correctly..

    ReplyDelete

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.