You are on page 1of 11

//Lab-2

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

int number=0,num=0;
long int s[120];

int cmp(const void *p, const void *q)


{
return (*(int*)p-*(int*)q);
}

void swap(char *A,char *B)


{
char w;
w=*A;
*A=*B;
*B=w;
}

//function to print different combinations


void combination1(char *a,int t,int n)
{
int k;
if(t==n)
{
for(k=0;k<5;k++)
printf("%c",a[k]);
printf("-");
number++;
s[num++]=((long int)10000*((*a)-48))+1000*(*(a+1)-48)+100*(*(a+2)-
48)+10*(*(a+3)-48)+1*(*(a+4)-48);
if(a[4]%2==0)
printf("sum is even\n");
else
printf("sum is odd\n");
}
else
{
for(k=t;k<=n;k++)
{
swap((a+t),(a+k));
combination1(a,t+1,n);
swap((a+t),(a+k));
}
}
}
int count(char *ptr) //function to count number of characters in string
{
int j=0,l;
for(l=0;ptr[l]!='\0';l++)
j++;
return j;
}

int main()
{
FILE *fptr,*fp;
int age,i,j=0,N;
char name[20],name1[20],digit[6],ch,str[10];
fptr=fopen("program2.txt","w");
if(fptr==NULL)
{
printf("error to create file");
exit(1);
}

printf("A\nN\nK\nI\nT\n");
printf("2*0*5*1*2*1*0*2*2");
printf("\n06/05/1999\n");
scanf("%d",&age);
printf("my 50th birthyear is %d",(50-age)+2021);
printf("\n%d,%d,%d,%d\n",1999+50,1999+100,1999+150,1999+200);
scanf("%s",name);
for(i=0;name[i]!='\0';i++) //to make set of characters in name
{
if(name[i]!=' ')
name1[j++]=name[i];
}
name1[j]='\0';
N=count(&name1[0]);
printf("%d\n",N);

for(i=0;i<5;i++) //to write different combinations of name in file


{
ch=name1[i];
name1[i]=name1[i+1];
name1[i+1]=ch;
for(j=0;j<N;j++)
fprintf(fptr,"%c",name1[j]);
fprintf(fptr," ");
}
fclose(fptr);
fptr=fopen("program2.txt","a");
fprintf(fptr,"##");
fclose(fptr);
fptr=fopen("program2.txt","r");

//create csv file


fp=fopen("output.csv","w");
while((ch=fgetc(fptr))!='#')
{
if(ch==' ')
fprintf(fp,",");
else
fprintf(fp,"%c",ch);
}
fclose(fptr);
fclose(fp);

for(i=0;i<5;i++)
scanf(" %c",&digit[i]);
digit[6]='\0';
printf("\nPossible combinations are:\n");
combination1(digit,0,4);
printf("\nSorted list is:\n");

qsort(s,number,sizeof(long int),cmp); //create sorted list


for(i=0;i<number;i++)
printf("%ld\n",s[i]);
return 0;
}
Output :-

You might also like