Tuesday, 12 April 2011

C prog for Reversing and checking an integer is a palindrome or not


#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include <math.h>
int Palindrome(int);

void main()
{
int a=0,b=0;
long int n=1;
int k;
int  numdigit=5;
//for palindrome

printf("Enter the any  number");
scanf("%d",&a);
k=Palindrome(a);
if(k==1)
{
printf("The given number is a palindrome:\n");
}
else
printf("The given number is not  a palindrome:\n");



_getch();
}

int Palindrome(int a1)
{
int temp=1,remainder=0,temp1=0,a2=0;
int a3=0,count=1,a5=0;
a3=a1;
a5=a1;
for(int i=0;i<100;i++)
{

int a4=a3/10;
a3=a4;

if(a3==0)
{
a2=count;
break;
}
else
count++;
a2=count;
}

int* arr={(int*)malloc(sizeof(int)*a2)};

for(int i=0;i<(a2+1);i++)
{
temp= a1/10;
remainder=a1-temp*10;
*(arr+i)=remainder;
a1=temp;
if (temp==0)
break;
}


for(int i=0;i<(a2+1);i++)
{
temp1+=(*(arr+i))* pow (10.0f,(a2-(i+1)));
if(a2-(i+1)==0)
break;
}

if (temp1==a5)
{
return 1;
}

else

return 0 ;
}

Monday, 11 April 2011

C++ String Class

In C++ String Class is provided for manipulation of strings . It is more user friendly .The functions of this class can be accessed using a  (".") operators.
To include this class we have to use 
using namespace std
------------------------------------------------------------
To initialize a String
------------------------------------------------------------ 
string a="This is my code";
------------------------------------------------------------------------
Functions Provided In String class

find()-  This function returns the position of a particular character .
substr()- This function returns the desired string with in a string.
length()-This Function returns the number of character in the given string.
compare()- This function compare two string and return a bool result.
find_first_of()- This functions find the first occurring of a character in the string
find_last_of()This functions find the first occurring of a character in the string


////////////////////////////////////////////////////////////////////////////////////////////////////////
Program to find the number of vowels in a string
*******************************************************************

long numvowel(string a1)
{
int count=0;
string str1;
string::iterator it;
for(it=a1.begin() ; it < a1.end();it++)
{
str1=*it;
if(str1.compare("a")==0 || str1.compare("A")==0|| str1.compare("e")==0|| str1.compare("E")==0|| str1.compare("i")==0|| str1.compare("I")==0|| str1.compare("A")==0|| str1.compare("o")==0|| str1.compare("O")==0|| str1.compare("u")==0|| str1.compare("U")==0)
count++;

}
else 
count;
}
return count;

}
****************************************************************************
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function to reverse a string 
********************************************************************************

string reverse(string a1)
{
string temp;
string str1;
string::reverse_iterator rit;
int i=0;
for ( rit=a1.rbegin() ; rit < a1.rend(); rit++ )

{
  temp=*rit;
  str1.append(temp);
  i++;

}


return str1;
}
**********************************************************************************
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


To check if a given string is palindrome or not


*************************************************************************

string palindrome(string a1)
{
string temp;
string str1;
int i=0;
string::reverse_iterator rit;
str1=reverse(a1);

i=str1.compare(a1);
if(i==0)
{
return "TRUE";
}
else

return "False";
}
*************************************************************************
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Function to find number of blank space in a string
***********************************************************************

int blankspace(string a1)
{
string str1;
int count=0;
string::iterator it;
for(it=a1.begin() ; it < a1.end();it++)
{
str1=*it;


if(str1.compare(" ")==0)
{
count++;
}
else
count;
}

return count;

}
**************************************************************************


Function prototype
long numchar(string);
int blankspace(string);
long numwords(string);
long numvowel(string);
string reverse(string);
string palindrome(string);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


You must have to include using namespace std;

 main function which call all this function
**************************************************************************
void main()
{
string a="This code is primarily developed to understanding from basic";
string str1,str2;
string::iterator i;
printf("The number of charactres in this string is:%d \n",numchar(a));
printf("The number of blankspace in this string is:%d \n",blankspace(a));
printf("The number of vowels in this string is:%d \n",numvowel(a));
str1=reverse(a);
for(i=str1.begin();i<str1.end();i++)
{
printf("%c",*i);


}
str2=palindrome(a);
printf("\n Given String is palindrome  :");
for(i=str2.begin();i<str2.end();i++)
{
printf("%c",*i);


}
_getch();
}
****************************************************************************
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



Sunday, 10 April 2011

Printing certain patterns of number in the console window

                                                              "problem 1"
for (int i=1;i<7;i++)
{
for(int j=1;j<i+1;j++)
{
printf(" %d \t",j);
}
printf("\n");
printf("\n");

}
printf("\n");
printf("\n");

*****************************************************************************
                                                //Problem 2:
printf("problem 2");

for (int i=1;i<7;i++)
{
for(int j=1;j<i+1;j++)
{
printf(" %d \t",i);
}
printf("\n");
printf("\n");

}

****************************************************************************

                                                                       //problem 3
for (int i=1;i<7;i++)
{
for(int j=0;j<i;j++)
{
printf(" %d \t",i-j);
}
printf("\n");
printf("\n");

}
printf("\n");
printf("\n");





***************************************************************************
                                                          "Problem 4"
for (int i=1;i<6;i++)
{
int blankspace=0;
blankspace=6-i;
for(int k=0;k<blankspace-1;k++)
{
printf("  \t");
}
for(int j=0;j<i;j++)
{
printf("%d\t",j+1);
}
printf("\n");
printf("\n");
}

**************************************************************************


                                                                  "Problem 5"
printf("\n");
printf("\n");
for (int i=1;i<6;i++)
{
int blankspace=0;
blankspace=6-i;
for(int k=0;k<blankspace-1;k++)
{
printf("  \t");
}
for(int j=0;j<i;j++)
{
printf("%d\t",i);
}
printf("\n");
printf("\n");

}
                                           
********************************************************************************
                                                                     "Problem 6"
for (int i=1;i<6;i++)
{
int blankspace=0;
blankspace=6-i;
for(int k=0;k<blankspace-1;k++)
{
printf("  \t");
}
for(int j=0;j<i;j++)
{
printf("%d\t",i-j);
}
printf("\n");
printf("\n");

}
                                                  
**************************************************************************
                                                  "Problem 7"
for (int i=0;i<5;i++)
{
for(int j=5;j>=i+1;j--)
{
//printf(" \t");
printf(" %d \t",6-j);
}
printf("\n");
printf("\n");

}
                                           
****************************************************************************
                                                     "Problem 8"
for (int i=1;i<6;i++)
{
for(int j=6;j>=i+1;j--)
{
//printf(" \t");
printf(" %d \t",i);
}
printf("\n");
printf("\n");

}                  
                                          

******************************************************************************
                                                         "Problem 9"
for (int i=1;i<6;i++)
{
for(int j=6-i;j>0;j--)
{
//printf(" \t");
printf(" %d \t",6-i);
}
printf("\n");
printf("\n");

}
                                        
*****************************************************************************
                                       "Problem 10"
for (int i=1;i<6;i++)
{
for(int j=6-i;j>0;j--)
{
//printf(" \t");
printf(" %d \t",j);
}
printf("\n");
printf("\n");

}
                                                    

**************************************************************************
                                                "Problem 11"
for (int i=1;i<6;i++)
{
for(int j=6;j>=i+1;j--)
{
//printf(" \t");
printf(" %d \t",j-1);
}
printf("\n");
printf("\n");

}

                                        
********************************************************************************
                                           "Problem 22"
for (int i=1;i<6;i++)
{
for(int j=1;j<i+1;j++)
{
printf(" %d \t",6-i);
}
printf("\n");
printf("\n");

}


****************************************************************************