Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, March 30, 2016

C program to print all the posstion permutations of the string

#include<stdio.h>
#include<conio.h>
#include<string.h>
int factorial(int x)
 {
  if(x==1||x==0)
   {
    return 1;
   }
  else
   {
    return x*factorial(x-1);
   }
 }
void main()
{
 clrscr();
 char str[]="ABCD";
 int i,j,k,l,count=0;
 char temp;
 /*for(i=0;i<4;i++)
  {
   for(j=0;j<3;j++)
    {
     for(k=0;k<2;k++)
      {
       for(l=0;l<1;l++)
{
printf("%d %d \n",i,j);
count=count+1;
}
      }
    }
  }*/
  l=strlen(str);
  j=0;
  k=1;
  for(i=0;i<factorial(l);i++)
   {
    if(j==l)
     {
      j=0;
     }
    if(k==l)
     {
      k=0;
     }
    temp=str[j];
    str[j]=str[k];
    str[k]=temp;
    printf("%s\n",str);
    j++;
    k++;
   }
 //printf("count %d",count);
 getch();
}

output

Tuesday, March 15, 2016

C program to convert a digit into words

#include<stdio.h>
#include<conio.h>
#include<string.h>
char *single_digit[]={"","one","two","three","four","five","six","seven","eight","nine"};
char *two_digit[]={"ten","eleven","twelve","thirteen","fourteen","fiveteen","sixteen","seventeen","eighteen","nineteen"};
char *tens_multiple[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
char *tens_power[]={"hundred","thousand"};
void one(char *num1)
{
 printf("%s",single_digit[*num1-'0']);
}
void two(char *num2)
{
 if(*num2=='0')
 {
  num2=num2+1;
  one(num2);
 }
 else if(*(num2)=='1')
      {
       printf("%s",two_digit[*(num2+1)-'0']);
      }
     else
      {
       printf("%s ",tens_multiple[*num2-'0']);
       num2=num2+1;
       one(num2);
      }
}
void three(char *num3)
{
 if(*num3=='0')
  {
   num3=num3+1;
   two(num3);
  }
 else
  {
   one(num3);
   printf("%s "," hundred");
   num3=num3+1;
   two(num3);
  }
}
void four(char *num4)
{
 if(*num4=='0')
  {
   num4=num4+1;
   three(num4);
  }
 else
  {
   one(num4);
   printf("%s "," thousand");
   num4=num4+1;
   three(num4);
  }
}
void convert(char *number)
{
 //printf("\nNumber is %s length is %d",number,strlen(number));
 while(*number=='0')
  {
   number=number+1;
  }
  //printf("\nnumber is %s length is %d",number,strlen(number));
  int l=strlen(number);
  //printf("\n first number is %s ",single_digit[0]);
  switch(l)
   {
    case 1:
     one(number);
     break;
    case 2:
     two(number);
     break;
    case 3:
     three(number);
     break;
    case 4:
     four(number);
     break;
    default:
     printf("\nOnly four digit numbers are allowed");
    }
}
void main()
 {
  char *num;
  printf("\n Enter a number ");
  scanf("%s",num);
  //printf("String is %s",num);
  convert(num);
  getch();
 }

output
Enter a number 1230
one thousand two hundred thirty