Question 1:
#include<stdio.h>
int main()
{
int arr1[100], arr2[100];
int n1,n2;
int check = 1;
int i;
int k;
scanf("%d",&n1);
for( i = 0; i < n1; i++)
scanf("%d",&arr1[i]);
scanf("%d",&n2);
for( i = 0; i < n2; i++ )
scanf("%d",&arr2[i]);
for(i = 0; i < n1; i++)//sorting arr1.
{
for(int j = i; j < n1; j++)
{
if(arr1[j]>arr1[i])
{
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
for (int k = 0; k < n1; k++)//comparing arr1 and arr2.
{
check = 1;
for(i = 0; i < n2; i++)
{
if(arr1[k] == arr2[i])
{
check = 0;
break;
}
}
if(check == 1)
{
printf("%d",arr1[k] );
break;
}
}
if(check == 0)
printf("0");
return 0;
}
Question 2:
#include<stdio.h>
int main()
{
int arr[100];
int n1,n2;
int counter = 0;
int i;
scanf("%d",&n1);
for( i = 0; i < n1; i++)
scanf("%d",&arr[i]);
for(i = 0; i < n1; i++)
{
for(int j = i; j < n1; j++)
{
if(arr[j]>arr[i])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for( i = 0; i < n1; i++)
{
if(arr[i] != arr[i+1])
counter++;
}
printf("%d",counter );
return 0;
}
Question 3:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* replaceWord(const char* s, const char* oldW,
const char* newW)
{
char* result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], oldW) == &s[i]) {
cnt++;
i += oldWlen - 1;
}
}
result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);
i = 0;
while (*s) {
if (strstr(s, oldW) == s) {
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}
result[i] = '\0';
return result;
}
int main()
{
char str[100];
char c[2];
char d[10];
char* result = NULL;
scanf("%s",str);
scanf("%s",c);
scanf("%s",d);
result = replaceWord(str, c, d);
printf("%s", result);
free(result);
return 0;
}