Tuesday 6 June 2017

Secure the given valid id's by making 1st 9 digit as *.

Secure the given valid id's by making 1st 9 digit as *.
valid id is identified by following condition.
1) Even places of a number sum is greater than the odd places sum.
2)It's consists of 13 digit

#include<stdio.h>
#include<string.h>
int main()
{
 int count=0,odd=0,even=0,i,start=-1,end=-1;

 char a[100];
 int j=0;
 gets(a);
 printf("%s\n",a);
  for(i=0;i<strlen(a);i++)
  {
    if(a[i]>='0'&&a[i]<='9')
    {
            if(count==0){start=i;}
        if(count%2==0)
        {odd=odd+(a[i]-'0');}
        else
        {even=even+(a[i]-'0');}
        if(count==8){end = i;}
     ++count;
    }
    if(count==13&&even>=odd)
    {
   
    for(j=start;j<=end;j++)
        {a[j]='*';}
count=0;even=0;odd=0;start=0;end=0;
    }
    else if(count==13){ count=0;even=0;odd=0;start=0;end=0;}
  }
 // printf("%d %d %d %d\n",start,end,even,odd);

  for(i=0;i<strlen(a);i++)
  {
      printf("%c",a[i]);
    }
 return 0;
}

OUTPUT:

Write a program to sort the elements in odd positions in descending order and elements in ascending order

Eg 1: Input: 13,2 4,15,12,10,5
        Output: 13,2,12,10,5,15,4
Eg 2: Input: 1,2,3,4,5,6,7,8,9
        Output: 9,2,7,4,5,6,3,8,1 
#include<stdio.h>
int main()
{
    int n,i,k=0,j,t=0,t1;
    scanf("%d",&n);
    int a[n],b[n/2],c[n/2];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(i%2==0)
        {b[k]=a[i];++k;}//printf("%d ",a[i]);
        else
        {c[t]=a[i];++t;} 
    }
    for(i=0;i<t;i++)
     for(j=0;j<t;j++)
          if(c[i]<c[j])
           {
               t1=c[i];
               c[i]=c[j];
               c[j]=t1;
           }
     for(i=0;i<k;i++)
      for(j=0;j<k;j++)
         if(b[i]>b[j])
           {
               t=b[i];
               b[i]=b[j];
               b[j]=t;
           }   
   k=0;t=0;
    for(i=0;i<n;i++)
    {
        if(i%2==0)
           printf("%d ",b[k]);++k;
       else
            printf("%d ",c[t++]);
    }
    return 0;
}

Write a program to give the following output for the given input.(Encoding)

Input:  aaaabcccc
Output: a4b1c4
Input: bccdddeeee
Output: b1c2d3e4
#include<stdio.h>
#include<string.h>
int main()
{
int n,count=1,i,j;
char a[50],ch;
scanf("%s",a);
ch=a[0];
for(i=1;i<strlen(a);i++)
{
if(ch==a[i])
++count;
else {
printf("%c%d",ch,count);count=1;ch=a[i];}
if(i==strlen(a)-1)
printf("%c%d",ch,count);
}
return 0;
}


Write a program to give the following output for the given input.(Decoding)

Eg 1: Input: a1b10
Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j,count=0;
char a[50],ch;
scanf("%s",a);
for(i=0;i<strlen(a);i++)
{
if(a[i]>='0'&&a[i]<='9')
{
count=(count*10)+(a[i]-'0');
}
else if(count>0)
{ count--;
for(j=0;j<count;j++)
{
printf("%c",ch);
}count=0;
}
if(a[i]>'9')
{
ch=a[i];printf("%c",a[i]);
}
// printf("%d\n",i);
if(i==(strlen(a)-1))
{--count;
for(j=0;j<count;j++)
{
printf("%c",ch);
}
}
}

return 0;
}

Write a program to give the following output for the given input.a1b10 (Decoding)

Eg 1: Input: a1b10
Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,j,count=0;
char a[50],ch;
scanf("%s",a);
for(i=0;i<strlen(a);i++)
{
if(a[i]>='0'&&a[i]<='9')
{
count=(count*10)+(a[i]-'0');
}
else if(count>0)
{ count--;
for(j=0;j<count;j++)
{
printf("%c",ch);
}count=0;
}
if(a[i]>'9')
{
ch=a[i];printf("%c",a[i]);
}
// printf("%d\n",i);
if(i==(strlen(a)-1))
{--count;
for(j=0;j<count;j++)
{
printf("%c",ch);
}
}
}

return 0;
}

Monday 5 June 2017

Alternate sorting

Alternate sorting: Given an array of integers, rearrange the array in such a way that the first element is first maximum and second element is first minimum.
    Eg.) Input  : {1, 2, 3, 4, 5, 6, 7}
Output : {7, 1, 6, 2, 5, 3, 4}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,t,j;
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int)); // creating dynamic array
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//sorting
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
t=n-1;j=0;
//printing alternate min max
for(i=0;i<n;i++)
{
if(i%2!=0){
printf("%d ",a[j] ); ++j;}
else{
printf("%d ",a[t] ); --t;}
}

return 0;
}
OUTPUT : 


Search for substring in two dimensional array

Save the string “WELCOMETOZOHOCORPORATION” in a two dimensional array and search for substring like “too” in the two dimensional string both from left to right and from top to bottom.
W E L C O
M E T O Z
O H O C O
R P O R A

T I O N
And print the start and ending index as
Start index : <1,2>
End index: <3, 2>

#include<stdio.h>

#include<string.h>

 int i,j,cols,k=-1;

 char a[50],b[50][50],t=0,search[50]; 

void searchword(char b[50][50],char search[50],int i1,int j1 )  //searching given substring



 int i=0,j=j1,end1,end2,found=0;

  for(i=0;i<strlen(search);i++)

  {

     if(b[i1][j1]==search[i]){//printf("%c",search[i]);

        end1=i1;end2=j1;++j1;++found;

     }

     else {

     break;}

  }

     if(found==strlen(search))

     {    printf("start index : <%d,%d>\n",i1+1,j+1);

          printf("end index   : <%d,%d>\n",end1+1,end2+1);found=0;

           } j1=j;j=i1;found=0;

  for(i=0;i<strlen(search);i++)

  {

    if(b[i1][j1]==search[i]){

     end1=i1;end2=j1;++i1;++found;}

    else {break;  }

  }     

  if(found==strlen(search))

   { printf("start index : <%d,%d>\n",j+1,j1+1);

     printf("end index   : <%d,%d>\n",end1+1,end2+1);found=0;} 

}

int main()

{

 scanf("%s",a);       // input string 

 scanf("%d",&cols);   //column width

 scanf("%s",search);   //substring

  for(i=0;i<=strlen(a)/cols;i++)

  {

    for(j=0;j<cols;j++)

    {  

        b[i][j]=a[++k];printf("%c ",b[i][j]);

    }

printf("\n");

  }

 k=0;

 for(i=0;i<=strlen(a)/cols;i++)

 {

     for(j=0;j<cols;j++)

     {

         if(b[i][j]==search[0])

         {

             searchword(b,search,i,j);   // pass 1st indexes present in given matrix

         }

     }

 }

 return 0;

 }


 OUTPUT :







To find sum of weights in a given array

  Given a set of numbers like <10, 36, 54,89,12> we want to find sum of weights based on the following conditions
    1. 5 if a perfect square
    2. 4 if multiple of 4 and divisible by 6
    3. 3 if even number

And sort the numbers based on the above condition and print it as follows
<10,its_weight>,<12,its weight><36,its weight><54,its weight>
Should display the numbers based on increasing order.


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
int n,i,j,t1,t2;
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int)); //create dynamic array for size n
int *b=(int *)calloc(n,sizeof(int)); // set all weightage to zero
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//to check and find weightage
for(i=0;i<n;i++)
{
if(sqrt(a[i])*sqrt(a[i])==a[i])
{
b[i]+=5;
}
if(a[i]%4==0&&a[i]%6==0)
{
b[i]+=4;
}
if(a[i]%2==0)
{
b[i]+=2;
}
}
// to sort number in ascending
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
t1=a[i];t2=b[i];
a[i]=a[j];b[i]=b[j];
a[j]=t1;b[j]=t2;
}
}
}
//printing the weightage
for(i=0;i<n;i++)
{
if(b[i]!=0)
printf("<%d,%d> ",a[i],b[i]);
}
return 0;
}

OUTPUT : 


   

C program to print X pattern


#include<stdio.h>
#include<string.h>
int main()
{
int i,j;
char a[50];
scanf("%s",a);
int t=strlen(a)-1; //set right side diagonal start value
for(i=0;i<strlen(a);i++)
{
for(j=0;j<strlen(a);j++)
{
if(i==j&&j==t){
printf("%c",a[i]);--t; // to print mid value
}
else if (i==j){
printf("%c",a[i]); // to print left to right diagonal
}
else if(j==t&&j!=i){
printf("%c",a[t]);--t;} // to print right to left diagonal
else
printf(" "); // to print spaces between spaces
}
printf("\n"); // to print on new line
}
return 0;
}
OUTPUT :