"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Library Function. Show all posts
Showing posts with label Library Function. Show all posts

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();
}

Friday, January 8, 2010

A menu driven program to enter a text(word / sentance) and convert it into Uppercase , Lowercase, Togglecase as per the user choice.


#include<iostream.h>
#include<conio.h>
#include<string.h>
{
char str[30], choice;
cout<<"\n Enter A String :";
cin.getline(str,30);
int t,i;


do{
cout<<"*************** MENU ********************\n";
cout<<"\n\n 1. UPPERCASE.";
cout<<"\n 2. LOWERCASE.";
cout<<"\n 3. TOGGLECASE.";


cout<<" Enter your choice (1-2-3)";
cin>>t;


switch(t)
{
case 1:
           for( i = 0; str[i] != '\0' ; i++)
                  str[i] = toupper(str[i]);
           cout<< "\n The output =" << str;


           break;
case 2:


          for( i = 0; str[i] != '\0' ; i++)
              str[i] = tolower(str[i]);
          cout<< "\n The output =" << str;


          break;
case 3:
          for( i = 0; str[i] != '\0' ; i++)


         {            
          
            if(isupper(str[i])
               str[i] = tolower(str[i]);
            if(islower(str[i])


               str[i] = toupper(str[i]);
            }
           cout<< "\n The output =" << str;
            break;
default:
             cout<<"\n Invalid choice.";
}


cout<<"\n Do you continue ?(Y/N)";
cin>>choice;
}while(toupper(choice)= ='Y');
}

Thursday, January 7, 2010

A program that reads a string & a character . Each occurrence of the given character (regardless of its case) is converted to opposite case.

#include(iostream.h)
#include(conio.h)
#include(string.h)


void main()
{
char str[30], ch, ch1;


cout<<"\n Enter a String:";
cin.getline(str,30);


cout<<"\n Enter a character :";
cin>>ch;


for(int i =0; str[i]!='\0'; ++i)
{
if(toupper(ch) = = toupper(str[i]))
  {
    if(str[i]>='A' && str[i]<='Z')
         str[i] = str[i] + 32;
     else
         str[i] = str[i] - 32;
}
}


cout<<"\n The Converted String is  "<< str;
getch();
}

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.