pd
BAN USER//Given two strings, find that the if the letters in both the strings are same? i.e. can we be able to make string2 out of string1 by shuffling the words and vice versa.
#include<stdio.h>
#include<string.h>
int isshuffled(char *str, char *shuff)
{
int i,s1[26] = {0}, s2[26] = {0},flag=1;
if(strlen(str) != strlen(shuff))
return 0;
for(i=0; i<strlen(str); i++)
{
s1[(int)str[i]-97] = s1[(int)str[i] - 97] + 1;
s2[(int)shuff[i]-97] = s2[(int)shuff[i]-97] + 1;
}
for(i=0; i<26; i++)
{
if(s1[i] != s2[i])
{
flag = 0;
return flag;
}
}
return flag;
}
int main()
{
if(isshuffled("apple","plpea"))
printf("\nShuffled string is made from original string");
else
printf("\nShuffled string is NOT made from original string");
return 0;
}
Ya that fine kamal. But there is one more method which is implemented up.. You need to start from left hand side i.i LSB and till 1 comes for the first time you just copy the same bits at the same location. Then after the 1, just flip the bits. This will give 2's coplement of a number.
E.g. 001100 --> 110100
//Given a binary number in string form, write a function to return its two's complement in string form.
#include<stdio.h>
#include<string.h>
void two_compl(char *s)
{
int i = strlen(s) - 1;
while(s[i] == '0')
{
i--;
}
i--;
while(i >= 0)
{
if(s[i]=='0')
s[i] = '1';
else
s[i] = '0';
i--;
}
printf("%s",s);
}
int main()
{
char s[] = "1101";
two_compl(s);
return 0;
}
//Write a function for reversing a doubly linked list.
#include<stdio.h>
struct node *head;
struct node
{
int val;
struct node *next;
struct node *prev;
};
void insert(int x, struct node *head)
{
struct node *p;
struct node *t = (struct node*) malloc (sizeof(struct node));
t->val = x;
t->next = t->prev = NULL;
p = head;
while(p->next != NULL)
{
p = p->next;
}
t->prev = p;
p->next = t;
}
void reverse(struct node *head)
{
struct node *p,*temp;
p = head;
while(p != NULL)
{
temp = p->prev;
p->prev = p->next;
p->next = temp;
p = p->prev;
}
head = temp->prev;
printf("\nReversed Values in the linked list are :\n");
p = head;
while(p != NULL)
{
printf("%d\t",p->val);
p = p->next;
}
}
void display(struct node *head)
{
struct node *p;
p = head;
while(p != NULL)
{
printf("%d\t",p->val);
p = p->next;
}
}
int main()
{
head = (struct node*) malloc (sizeof(struct node));
head->next = head->prev = NULL;
head->val = 4;
insert(5,head);
insert(6,head);
insert(7,head);
printf("\nValues in the linked list are :\n");
display(head);
reverse(head);
}
<code>//Find the centre and radius of a circle which passes through the maximum number of points. The centre must be one out of the given N points.
#include<stdio.h>
#include<math.h>
#define n 14
float dist(int *a,int *b)
{
float d;
d = sqrt(((*(b+1) - *(a+1)) * (*(b+1) - *(a+1))) + ((*(b) - *(a)) * (*(b) - *(a))));
return d;
}
float max_occurence(float a[],float *val)
{
int i,j;
float occ[n] = {0},max,v[n]={0};
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(a[i]==a[j])
{
occ[i]++;
v[i] = a[i];
}
}
}
max = occ[0];
for(i=0;i<n;i++)
{
if(occ[i]>=max)
{
max = occ[i];
*val = a[i];
}
}
return max;
}
float maxi(float d[])
{
int i,max = d[0],index;
for(i=0;i<n;i++)
{
if(d[i]>max)
{
max=d[i];
index=i;
}
}
return index;
}
void findcircle(int *a)
{
int i,j,c1=0,c2=0,index;
float r = 0, d[n]={0},temp[n]={0},val,radius[n]={0},tmp;
for(i=0;i<(2*n);i=i+2)
{
c2=0;
for(j=0;j<(2*n);j=j+2)
{
r = dist(a+i,a+j);
printf("Dist between (%d,%d) and (%d,%d) is %f \n",*(a+i),*(a+i+1),*(a+j),*(a+j+1),r);
temp[c2] = r;
c2++;
}
d[c1] = max_occurence(temp,&val);//finds the distance of all points from all the other points and find maximum occurence of distances
radius[c1]=val;
c1++;
}
index = maxi(d);
printf("Center of circle is (%d,%d)\n",*(a+(2*index)),*(a+(2*index)+1));
printf("Radius of circle is %f\n",radius[index]);
printf("Points on circle are:\n");
for(i=0;i<(2*n);i=i+2)
{
if(i!=(2*index))
{
tmp = (*(a+(2*index)) - *(a+i))*(*(a+(2*index)) - *(a+i)) + (*(a+(2*index)+1) - *(a+i+1))*(*(a+(2*index)+1) - *(a+i+1));
if(fabs(tmp - (radius[index]*radius[index])) <= 0.001 )//for floating point comparison
printf("(%d,%d)\n",*(a+i),*(a+i+1));
}
}
}
int main()
{
/*int a[n][2] = {{2,3},
{3,2},
{2,2},
{5,5},
{1,2},
};*/
int a[n][2] = {{2,4},
{3,3},
{3,5},
{4,2},
{4,4},
{4,6},
{5,3},
{5,5},
{6,4},
{2,2},
{2,3},
{3,2},
{3,4},
{4,3}
};
findcircle(&a[0][0]);
return 0;
}
</code>
Can u plz give the solution??
- pd May 20, 2012