Pointer Advanced QP
Pointer Advanced QP
Exercise No. : 6b
Code
#include<stdio.h>
#include<string.h>
int main(){
char str[255],*strs[20];
char* token;
char temp[255];
int i=0;
gets(str);
token=strtok(str,",");
while(token){
strs[i]=(char*)malloc(strlen(token)+1);
1
HANDS ON
strcpy(*(strs+i),token);
token=strtok(NULL,",");
i++;
}
for(int j=0;j<i;j++){
strcpy(temp,*(strs+j));
strrev(*(strs+j));
if(strcmp(temp,*(strs+j))==0){
printf("%s",temp);
return 0;
}
}
printf("No palindrome");
}
Output
hello,nothing,rar,noon
rar
You want to aggregate the data (addition) to get a complete
picture of the environment, and also compare the differences
(subtraction) to observe changes between two different sets of
data. However, the constraint is that the tool should return both
results (addition and subtraction) using pointers. This will save
memory by avoiding multiple function calls and make the code
2 efficient. Medium
Sample Input
Matrix 1
123
456
789
Matrix 2
987
654
321
Sample Output
Matrix
Addition: 10
10 10
2
HANDS ON
10 10 10
10 10 10
Matrix Subtraction:
-8 -6 -4
-2 0 2
4 6 8
Code
#include<stdio.h>
#include<stdlib.h>
void add(int a[3][3],int b[3][3],int n){
//int *s = (int*)malloc(9*sizeof(int));
printf("Matrix Addition:\n");
int s[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
*(*(s+i)+j)=a[i][j] + b[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d ",*(*(s+i)+j));
}
printf("\n");
}
}
void sub(int a[3][3],int b[3][3],int n){
//int *s = (int*)malloc(9*sizeof(int));
printf("Matrix Addition:\n");
int s[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
*(*(s+i)+j)=a[i][j] - b[i][j];
}
}
for(int i=0;i<n;i++){
3
HANDS ON
for(int j=0;j<n;j++){
printf("%d ",*(*(s+i)+j));
}
printf("\n");
}
}
int main(){
int n;
scanf("%d",&n);
int a[n][n],b[n][n];
printf("Matrix 1:\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("Matrix 2:\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&b[i][j]);
}
}
add(a,b,n);
sub(a,b,n);
}
Output
2
Matrix 1:
13
56
Matrix 2:
83
79
Matrix Addition:
96
15 3
4
HANDS ON
Matrix Addition:
-7 0
-3 1
You are working for a company that develops image
processing software. One of the common tasks in image
processing is rotating images by 90 degrees. Images can be
represented as 2D matrices where each element in the matrix
represents a pixel of the image. Your job is to implement a
matrix rotation feature in the software that can rotate a
square matrix (representing an image) 90 degrees clockwise.
Example:
3 Sample Input: [[1,2,3],[4,5,6],[7,8,9]] Medium
Sample Output: [[7,4,1],[8,5,2],[9,6,3]]
Overview:
Rotate the matrix simply by 90 degree clockwise and return
the matrix. 1 2 3
456
789
after rotating to 90 degree
741
852
963
Code
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n][n];
printf("Matrix :\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
printf("after rotating to 90 degree:\n");
for(int i=0;i<n;i++){
for(int j=n-1;j>=0;j--){
5
HANDS ON
printf("%d ",*(*(a+j)+i));
}
printf("\n");
}
}
Output
4
Matrix :
1472
3698
5126
8374
after rotating to 90 degree:
8531
3164
7297
4682
Find second-highest score
In a software application designed for a local sports club, the
club manager wants to analyze players' performance based
on their scores across various games. To recognize consistent
4 Medium
performers, the manager needs to find second-highest scores
among the players.
Your task is to write a C program that uses functions to
achieve this:
Define a function find_second_largest that takes an array of
player scores and its size as input.
Sample Input
Enter the number of scores: 5
Enter the scores:
45 67 89 23 67
Sample Output
The second-largest score is: 67
Sample Input
Enter the number of scores: 3
Enter the scores:
20 20 20
Sample Output
There is no second-largest score.
Sample Input
Enter the number of scores: 1
Enter the scores:
45
Sample Output
Not enough scores to determine the second largest.
Code
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
int main(){
int n;
printf("Enter the number of scores:\n");
scanf("%d",&n);
if(n==1){
printf("Not enough scores to determine the second largest");
return 0;
}
int *a=(int*)malloc(n*sizeof(int));
printf("Enter the scores:\n");
for(int i=0;i<n;i++){
scanf("%d",(a+i));
}
int max=INT_MIN;
7
HANDS ON
int sec=INT_MIN;
for(int i=0;i<n;i++){
if(max<*(a+i)){
sec=max;
max=*(a+i);
}
else if(sec<*(a+i)&&max>*(a+i))
sec=*(a+i);
}
if(sec==INT_MIN)
printf("There is no second-largest score.");
else
printf("%d",sec);
}
Output
Enter the number of scores:
3
Enter the scores:
10 20 20
10
Make Two strings to Anagram
Given two strings S1 and S2 in lowercase, the task is to make
them anagram. The only allowed operation is to remove a
character from any string. Find the minimum number of
characters to be deleted to make both the strings anagram.
5 Hard
Two strings are called anagrams of each other if one of them
can be converted into another by rearranging its letters.
Example 1:
Input:
S1 = bcadeh
S2 = hea
Output: 3
Explanation: We need to remove b, c and d from S1.
Example 2:
Input:
S1 = cddgk
8
HANDS ON
S2 = gcd
Output: 2
Explanation: We need to remove d and k from S1.
Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int c=0;
char s1[255],s2[255];
scanf("%s %s",s1,s2);
int m1[26]={0},m2[26]={0};
int l1=strlen(s1);
int l2=strlen(s2);
for (char *ptr = s1; *ptr != '\0'; ptr++) {
m1[*ptr - 'a']++;
}
for (char *ptr = s2; *ptr != '\0'; ptr++) {
m2[*ptr - 'a']++;
}
for(int i=0;i<26;i++){
c+=abs(m1[i]-m2[i]);
}
printf("%d",c);
}
Output
hjkl
kh
2
Isomorphic Strings
Problem statement : Given two strings 'str1' and 'str2', check if
these two strings are isomorphic to each other.
If the characters in str1 can be changed to get str2, then two
strings, str1 and str2, are isomorphic. A character must be
completely swapped out for another character while
maintaining the order of the characters. A character may map
to itself, but no two characters may map to the same
character.
Example 1:
Input:
str1 =
aab str2
6 Hard
= xxy
Output:
1
9
HANDS ON
Explanation:
There are two different characters in aab and xxy, i.e a and b
with frequency 2 and 1 respectively.
Example 2:
Input:
str1 =
aab str2
= xyz
Output:
0
Explanation:
There are two different characters in aab but there are three
different characters in xyz. So there won't be one to one
mapping between str1 and str2.
Code
#include <stdio.h>
#include <string.h>
int isiso(char *s1,char *s2){
int m1[256]={0},m2[256]={0};
int l1=strlen(s1);
int l2=strlen(s2);
if(l1!=l2)
return 0;
else
for(int i=0;i<l1;i++){
if(m1[(int)*s1]!=m2[(int)*s2])
return 0;
m1[(int)*s1]++;
m2[(int)*s2]++;
s1++;
s2++;
}
return 1;
}
int main(){
1
0
HANDS ON
char s1[255],s2[255];
scanf("%s %s",s1,s2);
printf("%d",isiso(s1,s2));
}
Output
rhy
jjk
0
Example 1:
Input: S1 = "ABCDGH", S2 = "ACDGHR", n = 6, m = 6
Output: 4
7 Hard
Explanation: The longest common
substring is "CDGH" which has length 4.
Example 2:
Input: S1 = "ABC", S2 "ACB", n = 3, m = 3
Output: 1
Explanation: The longest common
substrings are "A", "B", "C" all having
length 1.
Code
#include<stdio.h>//7
#include<string.h>
int main() {
char str1[20],str2[20],s[255][255];
printf("Enter string1:");
gets(str1);
printf("Enter string1:");
gets(str2);
int k=0;
int n=strlen(str1);
1
1
HANDS ON
int m=strlen(str2);
for(char *p1=str1;*p1!='\0';p1++){
for(char *p2=str2;*p2!='\0';p2++){
if(*p1==*p2){
int l=0;
char *t1=p1,*t2=p2;
s[k][l]=*p1;
while (*t1 == *t2) {
s[k][l++] = *t1;
t1++;
t2++;
}
k++;
}
}
}
int max=strlen(s[0]);
for(int i=0;i<k;i++){
if(max<strlen(s[i]))
max=strlen(s[i]);
}
printf("%d",max);
}
Output:
Enter string1:ABCDHIKLM
Enter string1:ABCHIKLMO
5
1
2