Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views5 pages

2023 PSP End-Sem QP & Ms

The document contains code snippets and explanations for various programming concepts, including recursive functions, string matching, and pointer manipulation in C. It highlights common errors in code and suggests corrections, such as memory allocation issues and string size requirements. Additionally, it discusses the behavior of string assignments and outputs of specific code segments.

Uploaded by

PRATIK GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

2023 PSP End-Sem QP & Ms

The document contains code snippets and explanations for various programming concepts, including recursive functions, string matching, and pointer manipulation in C. It highlights common errors in code and suggests corrections, such as memory allocation issues and string size requirements. Additionally, it discusses the behavior of string assignments and outputs of specific code segments.

Uploaded by

PRATIK GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CamScanner

CamScanner
Answer 1. A
int sumdigit(int num){
if (num/10==0)
return num;
else{
int digit = num%10;
return digit + sumdigit(num/10);
}
}

Answer 1.b

int strMatch(char *str, char *sub){


int index, len1, len2;
len1 = strlen(str);
len2 = strlen(sub);
if (len2>len1 || len2==0) { // DO NOT DEDUCT MARKS FOR THIS CONDITION
printf("Invalid Substring\n");
return -1; //substring is larger

}
printf("Original %s Substring %s\n", str, sub);
for(index=0;index<=len1-len2; index++){
int loc;
for(loc= 0; (loc<len2) && str[index+loc] ==sub[loc]; loc++)
;
if (loc==len2)
return index; //match found

}
printf("Match not found!\n");
return -1;

Answer 1.c
int add(int a, int b){
return a+b;
}
int main(){
int (*fptr)(int, int) = add;
printf("Addition:%d",(*fptr)(12,10));
return 0;
}
Sr. Questions Solutions
2(a) What is wrong in the following code and how to correct The pointer ip has reached the end of its allocated space
that? while reading the values through scanf. It needs to be
int *ip = (int *)malloc(20); brought back to beginning to print the input values.
int a[5]; Thus we need to save the starting address.
for(int i=0;i<5;i++) { int *ip = (int *)malloc(20), *ip2;
scanf("%d %d", ip++, &a[i]); int a[5];
//Input = 1 1, 2 2, 3 3, 4 4, 5 5 ip2 = ip;
} for (int i = 0; i < 5; i++) {
for(int i=0;i<5;i++) { scanf("%d %d", ip2++, &a[i]);
printf("%d, %d\n", (*ip)++, a[i]); // Input = 1 1, 2 2, 3 3, 4 4, 5 5
} }
ip2 = ip;
for (int i = 0; i < 5; i++) {
printf("%d, %d\n", (*ip2)++, a[i]);
}
2(b) char *cp = "Assessment"; Char str[10] has only 10 spaces while it needs 11 spaces
char str[10] = "Assessment"; to store the word and ending null character. So we need
printf("%s\n", cp); to declare array str to be of size 11 or more.
printf("%s\n", str);
For second code integer array cannot be created like a
Can we do the same as above for the following? string. Following code will not work.
int *ip = {1, 2, 3, 4, 5}; int *ip = {1, 2, 3, 4, 5};
int arr[10] = {1, 2, 3, 4, 5};
printf("%d\n", *ip);
printf("%d\n", *arr);
2(c) Look at the following code and specify the output (or error First code is correct. The strings will swapped. Output
if any): will be
char * temp; Shyam Ram.
char *str1 = "Ram", *str2 = "Shyam";
temp = str1; Second code is incorrect because arrays are contant
str1 = str2; pointers. They cannot be assigned another value while
str2 = temp; swapping.
printf ("%s %s\n", str1, str2);

What will change (or not) if I declare string as:


char str1[10] = "Ram", str2[10] = "Shyam";
2(d) What will be the output of following code: 5
int i, j, k = 0, *arr[3], *ptr; 123
for (i = 0; i < 3; i++) { 456
arr[i] = (int 789
*)malloc(3*sizeof(int));
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
arr[i][j] = ++k;
}
}
ptr = arr[1];
printf("%d\n", *(ptr + 1));
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

You might also like