If you like my posts please leave a comment.

Thursday, September 20, 2012

Some more programs using C++ Library Functions:



//program demonstrating the use of the functions - atoi(), atol(), itoa(), ltoa()
#include <iostream.h>
#include <stdlib.h>
int main()
{
int val1;//variable t store an integer value
long int val2;//variable to store a long integer value
int num1=12345;
long int num2=123456789;
char *str1="12345.33";
char *str2="23456789";
char string[30],string2[30];
val1=atoi(str1)+150;//adding 150 after converting the string to integer
val2=atol(str2)+1500;//adding 1500 after converting the string to long integer
itoa(num1,string,10);//converting an integer to string
ltoa(num2,string2,10);//converting an long int to string
cout<<"\n String was = "<<str1<<" and the new integer = "<<val1<<endl;
cout<<"\n String was = "<<str2<<" and the new long integer = "<<val2<<endl;
cout<<"\n Integer was = "<<num1<<" and the new string = "<<string<<endl;
cout<<"\n Integer was = "<<num2<<" and the new string = "<<string2<<endl;
return 0;
}
Output:



/*program to print the entered name in title case: This program is using strlen(), toupper(), tolower() functions*/
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
char fname[30];char lname[30];
int flength,llength;
char temp;

cout<<"\n Enter Your First Name: ";
cin>>fname;
fflush(stdin);
cout<<"\n Enter Your Second Name: ";
cin>>lname;
flength=strlen(fname);
llength=strlen(lname);
     for(int i=0;i<flength;++i)
   {
      if(i<flength)
      {
     if(i == 0)
     {
     temp=fname[i];
      fname[i]=toupper(temp);
      }
      else
      fname[i]=tolower(fname[i]);
   }
   for(int i=0;i<llength;++i)
   {

      if(i == 0)

       lname[i]=toupper(lname[i]);
       else
       lname[i]=tolower(lname[i]);

   }


}
strcat(fname," ");
strcat(fname,lname);
cout<<"\nHello! "<<fname;
getch();
return 0;
}
Output:

/*Program to count total number of alphabets, digits, spaces, other characters present in a line of text entered. This program is using isalpha(), isdigit(), isspace() functions */
#include <iostream.h>
#include <ctype.h>
#include<conio.h>
int main()
{
char line[100];
clrscr();
int alpha=0,digi=0,spc=0,other=0,chars=0;
cout<<"\n Enter a line of text: ";
cin.getline(line,100);
     for(int i=0;line[i]!=NULL;++i)
   {
     if(isalpha(line[i]))
      alpha++;
      else if(isdigit(line[i]))
      digi++;
      else if(isspace(line[i]))
      spc++;
      else
      other++;
   }
   chars = alpha+digi+spc+other;
cout<<"\n Total Alphabets Present = "<<alpha;
cout<<"\n Total Digits Present = "<<digi;
cout<<"\n Total Spaces Present = "<<spc;
cout<<"\n Total Other characters Present = "<<other;
cout<<"\nTotal = "<<chars;
getch();
return 0;
}
Output:

No comments:

Post a Comment