0% found this document useful (0 votes)
13 views6 pages

HCL Programm

The document contains code to check if two strings are anagrams of each other. It first checks if the lengths of the strings are equal. If so, it sorts both strings and then compares the characters of the strings to see if they are identical. If any characters do not match, it returns that the strings are not anagrams. Otherwise, it returns that the strings are anagrams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

HCL Programm

The document contains code to check if two strings are anagrams of each other. It first checks if the lengths of the strings are equal. If so, it sorts both strings and then compares the characters of the strings to see if they are identical. If any characters do not match, it returns that the strings are not anagrams. Otherwise, it returns that the strings are anagrams.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

anagrams

#include <stdio.h>

#include <string.h>

int main (void) {

char s1[10], s2[10];

char temp;

int i, j;

int n = strlen(s1);

int n1 = strlen(s2);

scanf("%s%s",s1,s2);

// If both strings are of different length, then they are not anagrams

if( n != n1) {

printf("%s and %s are not anagrams! \n", s1, s2);

return 0;

// lets sort both strings first −

for (i = 0; i < n-1; i++) {

for (j = i+1; j < n; j++) {

if (s1[i] > s1[j]) {

temp = s1[i];

s1[i] = s1[j];

s1[j] = temp;
}

if (s2[i] > s2[j]) {

temp = s2[i];

s2[i] = s2[j];

s2[j] = temp;

// Compare both strings character by character

for(i = 0; i<n; i++) {

if(s1[i] != s2[i]) {

printf("Strings are not anagrams! \n", s1, s2);

return 0;

printf("Strings are anagrams! \n");

return 0;

}
GCD

int gcd(int a, int b)

if (a == 0)

return b;

return gcd(b % a, a);

// Function to find gcd of array of

// numbers

int findGCD(int arr[], int n)

int result = arr[0];

for (int i = 1; i < n; i++)

result = gcd(arr[i], result);


if(result == 1)

return 1;

return result;

int main()

int arr[] = { 2, 4, 6, 8, 16 };

int n = sizeof(arr) / sizeof(arr[0]);

printf(“%d”,findGCD(arr, n) )

return 0; }
sYNTAX

#include <stdio.h>

#include <string.h>

char st[20];

int top=-1;

void psh(char);

char pop();

int main()

char a[20],t;

int i,f=1;

scanf("%s",a);

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

if(a[i]=='('||a[i]=='{'||a[i]=='[')

psh(a[i]);

if(a[i]==')'||a[i]=='}'||a[i]==']')

if(top==-1)

f=0;

else

{t=pop();

if(a[i]==')'&&(t=='['||t=='{'))

f=0;
if(a[i]=='}'&&(t=='('||t=='['))

f=0;

if(a[i]==']'&&(t=='{'||t=='('))

f=0;

if(top>=0)

f=0;

if(f==0)

printf("Unbalanced\n");

else

printf("Balanced\n");

return 0;

void psh(char a)

st[++top]=a;

char pop()

return st[top--];

You might also like