C/C++ Programming Questions:
1. Write a function revstr() - which reverses the given string in the same string buffer using pointers. (ie) Should not use extra buffers for copying the reverse string.
#include
char *rev_str(char *str)
{
char *s = str, *e = s + strlen(s) -1;
char *t = "junk"; /* to be safe - conforming with ANSI C std */
while (s < t =" *e;" len =" strlen(str),i=" j="len/2;">= 0 ( 1, 2, 4, 8, 16, .... ) without using C math library and arithmatic operators ( ie. *, /, +, - and math.h are not allowed)
#include void main(){ int i; for(i=0; i< na =" %d," b=" %d\n" t =" a;" a =" b;" b =" t;" a="10," b ="20;" e="10.0," f =" 20.0;" x = "string1" y = "string2" s1 =" {50," s2 =" {100," a =" (int" b =" (int" a =" 10;" b =" 20;" c =" (float" d =" (float" c =" 10.01;" d =" 20.02;">next == NULL) tail = head; if ((*head)->val == val) { *head = (*head)->next; } else head = &(*head)->next; } while((*tail)) { if ((*tail)->val == val) { *tail = (*tail)->prev; } else tail= &(*tail)->prev; }} /* Supporting (Verification) routine */ Link *DL_build();void DL_print(Link *); main(){ int val; Link *head; head = DL_build(); DL_print(head); printf("Enter the value to be deleted from the list : "); scanf("%d", &val); DL_delete(&head, val); DL_print(head);} Link *DL_build(){ int val; Link *head, *prev, *next; head = prev = next = NULL; while(1) { Link *new; printf("Enter the value for the list element (0 for end) : "); scanf("%d", &val); if (val == 0) break; new = (Link *) malloc(sizeof(Link)); new->val = val; new->prev = prev; new->next = next; if (prev) prev->next = new; else head = new; prev = new; } return (head);} void DL_print(Link *head){ Link *shead = head, *rhead; printf("\n****** Link List values ********\n\n"); while(head) { printf("%d\t", head->val); if (head->next == NULL) rhead = head; head = head->next; } printf("\n Reverse list \n"); while(rhead) { printf("%d\t", rhead->val); rhead = rhead->prev; } printf("\n\n");}
6 : What will be the output of this program ?
#include
main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i="0;"> savea = savea + sizeof(savea_type) * sizeof(int) ( by pointer arithmatic) => save = savea + sizeof(int) * sizeof(int) Note: You can verify the above by varing the type of 'savea' variable to char, double, struct, etc. Instead of statement 'savea += sizeof(int)' use savea++ thenthe values 0, 10, 20 and 30 will be printed. This behaviouris because of pointer arithmatic.
7 : Trace the program and print the output
#include
typedef int abc(int a, char *b);
int func2(int a, char *b)
{
a *= 2;
strcat(b, "func2 ");
return a;
}
int func1(int a, char *b)
{
abc *fn = func2;
a *= a;
strcat(b, "func1 ");
return (fn(a, b));
}
main()
{
abc *f1, *f2;
int res;
static char str[50] = "hello! ";
f1 = func1;
res = f1(10, str);
f1 = func2;
res = f1(res, str);
printf("res : %d str : %s\n", res, str);
}
Two function pointers f1 and f2 are declared of the type abc. whereas abc is a pointer to a function returns int.
func1() which is assigned to f1 is called first. It modifies the values of the parameter 'a' and 'b' to 100, "hello! func1 ". In func1(), func2() is called which further modifies the value of 'a' and 'b' to 200, "hello! func1 func2 " and returns the value of 'a' which is 200 to the main. Main calls f1() again after assigning func2() to f1. So, func2() is called and it returns the following value which will be the output of this program.
res : 400 str : hello! func1 func2 func2
The output string shows the trace of the functions called : func1() and func2() then again func2().
8 :
Write a program to reverse a Linked list within the same list
Solution:
#include typedef struct Link { int val; struct Link *next;} Link; /* Reverse List function */Link *SL_reverse(Link *head){ Link *revlist = (Link *)0; while(head) { Link *tmp; tmp = head; head = head->next; tmp->next = revlist; revlist = tmp; } return revlist;} /* Supporting (Verification) routines */ Link *SL_build(); main(){ Link *head; head = SL_build(); head = SL_reverse(head); printf("\nReversed List\n\n"); while(head) { printf("%d\t", head->val); head = head->next; }} Link *SL_build(){ Link *head, *prev; head = prev = (Link *)0; while(1) { Link *new; int val; printf("Enter List element [ 0 for end ] : "); scanf("%d", &val); if (val == 0) break; new = (Link *) malloc(sizeof(Link)); new->val = val; if (prev) prev->next = new; else head = new; prev = new; } prev->next = (Link *)0; return head;}
--------------------------------------------------------------------------------
9 : What will be the output of this program
#include
main()
{
int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
}
Solution:In C we can index an array in two ways. For example look in to the following lines int a[3] = {10, 20, 30, 40}; In this example index=3 of array 'a' can be represented in 2 ways. 1) a[3] and 2) 3[a]i.e) a[3] = 3[a] = 40 Extend the same logic to this problem. You will get theoutput as follows Hello! how is this? super That is C
1. Write a function revstr() - which reverses the given string in the same string buffer using pointers. (ie) Should not use extra buffers for copying the reverse string.
#include
char *rev_str(char *str)
{
char *s = str, *e = s + strlen(s) -1;
char *t = "junk"; /* to be safe - conforming with ANSI C std */
while (s < t =" *e;" len =" strlen(str),i=" j="len/2;">= 0 ( 1, 2, 4, 8, 16, .... ) without using C math library and arithmatic operators ( ie. *, /, +, - and math.h are not allowed)
#include void main(){ int i; for(i=0; i< na =" %d," b=" %d\n" t =" a;" a =" b;" b =" t;" a="10," b ="20;" e="10.0," f =" 20.0;" x = "string1" y = "string2" s1 =" {50," s2 =" {100," a =" (int" b =" (int" a =" 10;" b =" 20;" c =" (float" d =" (float" c =" 10.01;" d =" 20.02;">next == NULL) tail = head; if ((*head)->val == val) { *head = (*head)->next; } else head = &(*head)->next; } while((*tail)) { if ((*tail)->val == val) { *tail = (*tail)->prev; } else tail= &(*tail)->prev; }} /* Supporting (Verification) routine */ Link *DL_build();void DL_print(Link *); main(){ int val; Link *head; head = DL_build(); DL_print(head); printf("Enter the value to be deleted from the list : "); scanf("%d", &val); DL_delete(&head, val); DL_print(head);} Link *DL_build(){ int val; Link *head, *prev, *next; head = prev = next = NULL; while(1) { Link *new; printf("Enter the value for the list element (0 for end) : "); scanf("%d", &val); if (val == 0) break; new = (Link *) malloc(sizeof(Link)); new->val = val; new->prev = prev; new->next = next; if (prev) prev->next = new; else head = new; prev = new; } return (head);} void DL_print(Link *head){ Link *shead = head, *rhead; printf("\n****** Link List values ********\n\n"); while(head) { printf("%d\t", head->val); if (head->next == NULL) rhead = head; head = head->next; } printf("\n Reverse list \n"); while(rhead) { printf("%d\t", rhead->val); rhead = rhead->prev; } printf("\n\n");}
6 : What will be the output of this program ?
#include
main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
for (i=0; i<4; i="0;"> savea = savea + sizeof(savea_type) * sizeof(int) ( by pointer arithmatic) => save = savea + sizeof(int) * sizeof(int) Note: You can verify the above by varing the type of 'savea' variable to char, double, struct, etc. Instead of statement 'savea += sizeof(int)' use savea++ thenthe values 0, 10, 20 and 30 will be printed. This behaviouris because of pointer arithmatic.
7 : Trace the program and print the output
#include
typedef int abc(int a, char *b);
int func2(int a, char *b)
{
a *= 2;
strcat(b, "func2 ");
return a;
}
int func1(int a, char *b)
{
abc *fn = func2;
a *= a;
strcat(b, "func1 ");
return (fn(a, b));
}
main()
{
abc *f1, *f2;
int res;
static char str[50] = "hello! ";
f1 = func1;
res = f1(10, str);
f1 = func2;
res = f1(res, str);
printf("res : %d str : %s\n", res, str);
}
Two function pointers f1 and f2 are declared of the type abc. whereas abc is a pointer to a function returns int.
func1() which is assigned to f1 is called first. It modifies the values of the parameter 'a' and 'b' to 100, "hello! func1 ". In func1(), func2() is called which further modifies the value of 'a' and 'b' to 200, "hello! func1 func2 " and returns the value of 'a' which is 200 to the main. Main calls f1() again after assigning func2() to f1. So, func2() is called and it returns the following value which will be the output of this program.
res : 400 str : hello! func1 func2 func2
The output string shows the trace of the functions called : func1() and func2() then again func2().
8 :
Write a program to reverse a Linked list within the same list
Solution:
#include typedef struct Link { int val; struct Link *next;} Link; /* Reverse List function */Link *SL_reverse(Link *head){ Link *revlist = (Link *)0; while(head) { Link *tmp; tmp = head; head = head->next; tmp->next = revlist; revlist = tmp; } return revlist;} /* Supporting (Verification) routines */ Link *SL_build(); main(){ Link *head; head = SL_build(); head = SL_reverse(head); printf("\nReversed List\n\n"); while(head) { printf("%d\t", head->val); head = head->next; }} Link *SL_build(){ Link *head, *prev; head = prev = (Link *)0; while(1) { Link *new; int val; printf("Enter List element [ 0 for end ] : "); scanf("%d", &val); if (val == 0) break; new = (Link *) malloc(sizeof(Link)); new->val = val; if (prev) prev->next = new; else head = new; prev = new; } prev->next = (Link *)0; return head;}
--------------------------------------------------------------------------------
9 : What will be the output of this program
#include
main()
{
int a=3, b = 5;
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
}
Solution:In C we can index an array in two ways. For example look in to the following lines int a[3] = {10, 20, 30, 40}; In this example index=3 of array 'a' can be represented in 2 ways. 1) a[3] and 2) 3[a]i.e) a[3] = 3[a] = 40 Extend the same logic to this problem. You will get theoutput as follows Hello! how is this? super That is C
0 Comments:
Post a Comment
<< Home