Showing posts with label programs of string and pointers. Show all posts
Showing posts with label programs of string and pointers. Show all posts

Program to find the factorial of a large number

Thursday, September 10, 2009

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,k,a[200],temp,index,n,x,test;
cout<<"enter the no. of test cases";
cin>>test;
while(test>0)
{
cout<<endl<<"enter the number ";
cin>>num;
k=num;
for(i=0;k>=1;i++)
{
  a[i]=k%10;
  k=k/10;
    }


   for(n=1;n<num;n++)
   {
   temp=0;

      for(index=0;index<i;index++)
      {
            x=a[index]*n+temp;
            a[index]=x%10;
            temp=x/10;
            }

            while(temp!=0)
            {
               a[index]=temp%10;
               temp=temp/10;
               index++;
               i++;
                 }
}
for(i=index-1;i>=0;i--)
cout<<a[i];
test--;}
clrscr();
cout<<"made by anil";

        getch();



        }

calculate the occurance of a word in the string

Tuesday, September 1, 2009

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{  clrscr();
   char s1[100],s2[100];
   cout<<"enter the string:"<<endl;
   gets(s1);
   cout<<"enter the word: ";
   gets(s2);

   int i,j,k,p;
   i=j=k=0;
   while(s2[i]!='\0')
   {  j++;i++;       }

   for(i=0;s1[i]!='\0';)
   {     if(s1[i]==s2[k])
      {    k++;
           if(k==j)
           p++;
           continue;   }
                     }
   cout<<"number of times the word occur is: "<<p;


   getch();
   }

program of merge sort

void main()
{
 clrscr();
 int a[10],b[10],c[20],m,n,p,j;
 cout<<"enter the number of elements of first array: ";
 cin>>m;
 cout<<"enter the elements of first array"<<endl;
 for(int i=0;i<m;i++)
 cin>>a[i];
 cout<<"enter the number of elements of second array: ";
 cin>>n;
 cout<<"enter the elements of second array:"<<endl;
 for(i=0;i<n;i++)
 cin>>b[i];
 i=j=p=0;
 for(int l=0;l<m+n;l++)
 {
  if(i==m||j==n)
  { if(m<n)
    {for(int s=j;s<m+n;s++)
      {c[p]=b[j];
       p++;
       j++;}
     }
     else
     { for(int s=i;s<m+n;s++)
       {c[p]=a[i];
    p++;
    i++;}
      }
   }
  if(a[i]<b[j])
   { c[p]=a[i];
     p++;
     i++;
   }
   else
   {
    c[p]=b[j];
    p++;
    j++;
   }
  }
  cout<<"elements after merging";
  for(i=0;i<m+n;i++)
  cout<<c[i]<<" ";
  getch();
  }

program to show the use of gotoxy

#include <conio.h>

int main(void)
{
   clrscr();
   gotoxy(35, 12);
   cprintf("Hello world");
   gotoxy(10, 4);
   cprintf("Hello ");
   getch();
   return 0;
}

program which convert a float value to the string

#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
#include<iostream.h>

int main(void)
{ clrscr();
  char *str;
  double num;
  int dec, sign, ndig = 5;

  /* a regular number */
  num = 9.87654;
  str = ecvt(num, ndig, &dec, &sign);
  cout<<endl<<endl<<endl<<str<<endl<<endl<<endl;
  printf("string = %10s decimal place = %d sign = %d\n", str, dec, sign);
  getch();
  /* a negative number */
  num = -123.45;
  str = ecvt(num, ndig, &dec, &sign);
  printf("string = %10s decimal place = %d sign = %d\n", str, dec, sign);
  getch();
  /* scientific notation */
  num = 0.678e5;
  str = ecvt(num, ndig, &dec, &sign);
  printf("string = %10s  decimal place= %d  sign = %d\n", str, dec, sign);

  getch();
  return 0;

program to toogle a string

#include<iostream.h>
#include<conio.h>
void main()
{  clrscr();
   char s[50];
   cout<<"enter the string:"<<endl;
   cin>>s;
   int i=0;
   while(s[i]!='\0')
   {  if(s[i]>=65&&s[i]<=90)
      s[i]=s[i]+32;
      else
      s[i]=s[i]-32;
      i++;        }
   cout<<"string in toggle is:"<<endl;
   cout<<s;
   getch();
   }

program of dynamic sort

#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int n,*p;
  cout<<"enter the size of array: ";
  cin>>n;
  cout<<"enter the elements of array: ";
  p=new int[n];
  for(int i=0;i<n;i++)
  cin>>*(p+i);
  cout<<"values of array before sorting is: ";
  for(i=0;i<n;i++)
  cout<<*(p+i)<<" ";
  cout<<"\nvalues after sorting is: ";
  for(i=0;i<n;i++)
  {    int m;
       for(int j=i+1;j<n;j++)
       {   if(*(p+i)>*(p+j))
       { m=*(p+i);
         *(p+i)=*(p+j);
         *(p+j)=m;
       }
    }
  }
  for(i=0;i<n;i++)
  cout<<*(p+i)<<" ";
  getch();
 }

program of dynamic merge

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int n,m,*p,*q,*r;
  cout<<"enter the size of first array: ";
  cin>>n;
  p=new int[n];
  cout<<"enter the sorted elements of first array: ";
  for(int i=0;i<n;i++)
  cin>>*(p+i);
  cout<<"enter the size of second array: ";
  cin>>m;
  q=new int[m];
  cout<<"enter the sorted elements of second array: ";
  for(i=0;i<m;i++)
  cin>>*(q+i);
  r=new int[n+m];
  int a,b,c;
  a=b=c=0;
  for(i=0;i<n+m;i++)
  {  if(*(p+a)<*(q+b))
     {  *(r+c)=*(p+a);
    c++;
    a++;
    if(a==n)
    {  for(int j=b;j<m;j++)
       {  *(r+c)=*(q+b);
          c++;
          b++;
        }
        break;
    }
     }
     else
     {  *(r+c)=*(q+b);
    c++;
    b++;
    if(b==m)
    {  for(int j=a;j<n;j++)
       {  *(r+c)=*(p+a);
          c++;
          a++;
        }
        break;
    }
     }
  }
  cout<<"elements after merging is: ";
  for(i=0;i<m+n;i++)
  cout<<*(r+i)<<" ";
  getch();
  }

program to sort a string

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char x[100];
int n,i,j,m;
cout<<"enter the number of characters in the string"<<endl;
cin>>n;
cout<<"enter the string"<<endl;
cin>>x;
for(i=0;i<n;i++)
{
  for(j=0;j<n-i-1;j++)
  {  if(x[j]>x[j+1])
     { m=x[j];
       x[j]=x[j+1];
       x[j+1]=m;
     }
   }
 }
 cout<<"string in sort form: ";
 for(i=0;i<n;i++)
 cout<<x[i];
 getch();
 }

program to count vovels,digits,spaces,consonent and other characters in a string

#include<iostream.h>
#include<conio.h>
void main()
{
 char str[100];
 int vov=0,con=0,d=0,sp=0,oth=0;
 clrscr();
 cout<<"enter the string:"<<endl;
 cin.get(str,100);
 for(int i=0;str[i]!='\0';i++)
  {
  if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
  vov++;
  else if(str[i]>'a'&&str[i]<='z')
  con++;
  else if(str[i]>='0'&&str[i]<='9')
  d++;
  else if(str[i]==' ')
  sp++;
  else
  oth++;
  }
  cout<<"number of vovels "<<vov<<endl;
  cout<<"number of consonents "<<con<<endl;
  cout<<"number of digits "<<d<<endl;
  cout<<"number of spaces "<<sp<<endl;
  cout<<"number of other characters "<<oth<<endl;
  getch();
  }

program to count characters, words and lines in the string

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
 char a[100];
 int ch=0,w=0,l=0;
 cout<<"enter the string"<<endl;
 cin.get(a,100,'*');
 for(int i=0;a[i]!='\0';i++)
 {if(a[i]=='\n')
 { i++,w++;}
  if(a[i]==' ')
  w++;
  ch++;
  }
  cout<<"number of characters: "<<ch<<endl;
  cout<<"number of words: "<<w+1<<endl;
  cout<<"number of lines: "<<l+1<<endl;
  getch();
  }

program to reverse a string without using inbuilt function

#include<iostream.h>
#include<conio.h>
void main()
{  clrscr();
   char s[50];
   cout<<"enter the string:"<<endl;
   cin>>s;
   int i,m;
   i=0;
   while(s[i]!='\0')
   { i++;}
   for(int j=0;j<(i-1)/2;j++)
   {  m=s[j];
     s[j]=s[i-j-1];
     s[i-j-1]=m;        }

   cout<<"reverse string is:"<<endl;
   cout<<s;
   getch();
   }

program of selection sort

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[100],smal,n,temp;
 cout<<"enter the number of elements: ";
 cin>>n;
 cout<<"enter the elements: "<<endl;
 for(int i=0;i<n;i++)
 cin>>a[i];
 for(i=0;i<n;i++)
 {
  smal=a[i];
   for(int j=i+1;j<n;j++)
    {
     if(smal>a[j])
      {
    temp=smal;
    smal=a[j];
    a[j]=temp;
       }
     }
  a[i]=smal;
  }
  cout<<"elements after selection sort"<<endl;
  for(i=0;i<n;i++)
  cout<<a[i]<<" ";
  getch();
  }

program to remove the first and last occurance of a character in the string

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{  clrscr();
   char s[500],c;
   int l,i=0,p=0;
   cout<<"enter the string: \n";
   gets(s);
   cout<<"enter the character: ";
   cin>>c;
   l=strlen(s);
   while(s[i]!='\0')
   {  if(s[i]==c)
      {  for(int j=i;j<l;j++)
     { s[j]=s[j+1]; }
     p++;
     break;
      }
     i++;
   }
   i=l;
   while(i>=0)
   {  if(s[i]==c)
      {   for(int j=i;j<l;j++)
      {  s[j]=s[j+1];  }
      p++;
      break;
      }
      i--;
    }
    if(p==0)
    { cout<<"there is no occurance in the string \n";
      getch();
      exit(0);
    }
    if(p==1)
    cout<<"there is only one occurance at the begning which is removed \n";

    cout<<"string after romoving the occurance is: \n"<<s;
    getch();
    }

program to display a single character a the cursor position

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
 // putch(66);
  for(int i=0;i<256;i++)
  {
     textcolor(i);
     cputs("india   ");
  }
  getch();
  }

program to show the periority of increment or decrement operators

#include<iostream.h>
#include<conio.h>
#include<limits.h>
void main()
{       clrscr();
    int i,j;
    i=1;
    j=++i+(++i)+i+++(++i)+i+i+++i;
    cout<<j<<endl<<i;
    getch();
}

program to convert a string in uppercase without using inbuilt function

#include<iostream.h>
#include<conio.h>
void main()
{  clrscr();
   char s[50];
   cout<<"enter the string:"<<endl;
   cin>>s;
   int i=0;
   while(s[i]!='\0')
   {  if(s[i]>=97&&s[i]<=122)
      s[i]=s[i]-32;
      i++;        }
   cout<<"string in upper case is:"<<endl;
   cout<<s;
   getch();
   }

program to convert a string into lower case without using inbuilt function

#include<iostream.h>
#include<conio.h>
void main()
{  clrscr();
   char s[50];
   cout<<"enter the string:"<<endl;
   cin>>s;
   int i=0;
   while(s[i]!='\0')
   {  if(s[i]>=65&&s[i]<=90)
      s[i]=s[i]+32;
      i++;        }
   cout<<"string in lower case is:"<<endl;
   cout<<s;
   getch();
   }