Thursday, October 6, 2011

Example (String Program [II])




Find the Frequency of each word.


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

//Word Frequency

  void main()
      {
           char sent[100],word[20][20],tmp[20];
           int L,i,j,k,flag,cnt;
           char ch;
           clrscr();
           printf("Enter a Sentence : ");
           gets(sent);
           L=strlen(sent);
           printf("\nInput ; %s\n",sent);
           sent[L]=' ';
           k=0;
           for(i=0;i<L;i++)
              {
                   for(j=0;j<20;j++)
                        {
                            tmp[j]='\0';
                        }
                   j=0;
                   while(sent[i]!=' ')
                        {
                            tmp[j++]=sent[i];
                            i++;
                        }
                   tmp[j]='\0';
                   strcpy(word[k],tmp);
                   k++;
              }
     
           for(i=0;i<k;i++)
              {
                   cnt=1;
                   for(j=i+1;j<k;j++)
                        {
                            if(strcmpi(word[i],word[j])==0)
                                {
                                      cnt++;
                                }
                        }
                   flag=0;
                   for(j=i-1;j>=0;j--)
                        {
                            if(strcmpi(word[i],word[j])==0)
                                      flag=1;
                        }
                   if(flag==0)
                            printf("\nword :  %s Repeated %d times " ,word[i],cnt);
              }
           getch();
      }

Program for Case converter


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

//Case Converter

void Ucase(char a[])
      {
           int i;
           for(i=0;a[i]!='\0';i++)
              {
                   if(a[i]>=97 && a[i]<=122)
                        {
                            a[i]=a[i]-32;
                        }
              }
           printf("\n\t\tConverted to Uppercase %s\n",a);
      }
  void Lcase(char a[])
      {
           int i;
           for(i=0;a[i]!='\0';i++)
              {
                   if(a[i]>=65 && a[i]<=90)
                        {
                            a[i]=a[i]+32;
                        }
              }
           printf("\n\t\tConverted to Lowercase %s\n",a);
      }
  void Tcase(char a[])
      {
           int i;
           char b[100]=" ";
           strcat(b,a);
           strcat(b," ");
           for(i=0;b[i]!='\0';i++)
              {
                   if(b[i]==' ' && b[i+1]!=' ')
                        {
                            if(b[i+1]>=97 && b[i+1]<=122)
                                {
                                      b[i+1]=b[i+1]-32;
                                      i++;
                                }
                             else
                                {
                                      i++;
                                }
                        }
                    else if(b[i]>=65 && b[i]<=90)
                        {
                            b[i]=b[i]+32;
                        }
              }
           printf("\n\t\tConverted to Titlecase %s\n",b);
      }
  void Tgcase(char a[])
      {
           int i;
           for(i=0;a[i]!='\0';i++)
              {
                   if(a[i]>=65 && a[i]<=90)
                        {
                            a[i]+=32;
                        }
                   else if(a[i]>=97 && a[i]<=122)
                        {
                            a[i]-=32;
                        }
              }
           printf("\n\t\tToggle Case %s ",a);
      }

  void Scase(char a[])
      {
           int i,j;
           char b[100]=" ";
           strcat(b,a);
           strcat(b," ");
           for(i=0;b[i]!='\0';i++)
              {
                   if(b[i]==' ' && b[i+1]!=' ')
                        {
                            if(b[i+1]>=97 && b[i+1]<=122)
                                {
                                      b[i+1]=b[i+1]-32;
                                      i++;
                                }
                                break;
                         }

              }
           for(j=i+1;b[j]!='\0';j++)
              {
                   if(b[j]=='.' || b[j]=='?' || b[j]=='!')
                        {
                            if(b[j+1]==' ')
                                {
                                      if(b[j+2]>=97 && b[j+2]<=122)
                                                {
                                                          b[j+2]-=32;
                                                          j=j+2;
                                                }
                                      else
                                                          j=j+2;
                                }
                        }
                   else if(b[j]>=65 && b[j]<=90)
                        {
                            b[j]+=32;
                        }
              }
           printf("\n\t\tConverted to Titlecase %s\n",b);
      }

  void main()
      {
           char a[100];
           int c=0;
           while(c!=6)
              {
                   printf("\n\t1. UpperCase.\n");
                   printf("\t2. LowerCase.\n");
                   printf("\t3. Title Case.\n");
                   printf("\t4. Toggle Case.\n");
                   printf("\t5. Sentence Case.\n");
                   printf("\t6. Exit\n");
                   printf("\tEnter Your Choice : ");
                   scanf("%d",&c);
                   if(c<6)
                        {
                            fflush(stdin);
                            printf("\n\tEnter a String  : ");
                            gets(a);
                            printf("\n\t Input : %s",a);
                        }
                   switch(c)
                        {
                            case 1: Ucase(a);
                                                break;
                            case 2:  Lcase(a);
                                                break;
                            case 3: Tcase(a);
                                                break;
                            case 4: Tgcase(a);
                                                break;
                            case 5: Scase(a);
                                                break;
                            case 6: printf("\n\t  Quit .......");
                                                delay(50);
                                                exit(0);
                            default :printf("\n\t Wrong Choice.\n ");
                        }
              }
           getch();
      }


Convert Number to word


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

//Number to word
 void chk(int n)
  {
    char unit[20][20]={" ","One","Two","Three","Four","Five","Six","Seven","Eight", "Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen" , "Seventeen","Eighteen","Nineteen"};
    char tens[15][9]={"","","Twenty","Thirty","Forty","Fifty" , "Sixty", "Seventy", "Eighty", "Ninety"};
      if(n<20)
           printf("%s",unit[n]);
      else
           printf("%s %s ",tens[n/10],unit[n%10]);
  }

 void main()
  {
    long num,n,s=0,t=0;
    int r;
    int a,b,c=0;
    clrscr();
    printf("\nEnter number up to 9999999 ");
    scanf("%ld",&num);
    if(num>9999999 || num<0)
           printf("\n *  * * * Invalid Input * * * *\n");
    else if(num==0)
           printf("\nZero");
    else
      {
           printf("\n\n\t%ld  =  ",num);
           if(num>99999)
              {  n=num/100000;
                   chk(n);
                   printf(" Lakhs");
                   num=num%100000;
              }
           if(num>999)
              {  n=num/1000;
                   chk(n);
                   printf(" Thousands");
                   num=num%1000;
              }
           if(num>99)
              {  n=num/100;
                   chk(n);
                   printf(" Hundred");
                   num=num%100;
              }
           if(num>0)
              {
                   chk(num);
              }
       }
   getch();
  }

Same program can be done this way too but it is more complicated and suggest to use the above one.


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

//Number to word

void main()
  {
     long num,n,s=0,t=0;
     int r;
     char unit[20][20]={" ","One","Two","Three","four","five","six","seven","eight", "nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen " ,"seventeen","eighteen","nineteen"};
     char tens[15][9]={"","ten","twenty","thirty","forty","fifty" , "sixty", "seventy", "eighty", "ninety"};
    int a,b,c=0;
    clrscr();
    printf("\nEnter number up to 9999999 ");
    scanf("%ld",&num);
    if(num>9999999)
    printf("\n *  * * * Invalid Input * * * *\n");
    else
       {
    printf("\n\n%ld = ",num);
    n=num;
    while(n>0)
       {
            s=s*10+n%10;
            n/=10;
            c++;
       }
    if(c==7)
       {
            r=s%100;
            while(r>0)
       {
            t=t*10+r%10;
            r/=10;
        }
    if(t<20)
            printf("%s Lakhs ",unit[t]);
    else
         printf("%s %s Lakhs ",tens[t/10],unit[t%10]);
         s=s/100;
         c-=2;
      }

   if(c==6)
      {
         r=s%10;
         printf("%s Lakhs ",unit[r]);
         s=s/10;
         c--;
      }
  if(c==5)
     {
        t=0;
        r=s%100;
        while(r>0)
       {
          t=t*10+r%10;
          r/=10;
       }
        printf("%s %s thousands ",tens[t/10],unit[t%10]);
        s=s/100;
        c-=2;
     }
   if(c==4)
     {
        r=s%10;
        printf("%s thousands ",unit[r]);
        s=s/10;
        c-=1;
     }
        if(c==3)
      {
         r=s%10;
         printf("%s Hundred ",unit[r]);
         s=s/10;
         c-=1;
      }
        t=0;
        while(s>0)
      {
         t=t*10+s%10;
         s=s/10;
      }
        s=t;
        if(s<=20)
         printf("%s ",unit[s]);
        else
         printf("%s %s ",tens[s/10],unit[s%10]);
       }
    getch();
  }


No comments: