P. 1
Library Functions

Library Functions

|Views: 14|Likes:
Published by hydownstar

More info:

Published by: hydownstar on Oct 16, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

10/16/2010

pdf

text

original

*********************************************************************** ***************** LIBRARY FUNCTIONS

******************

*********************************************************************** 1] What do the functions atoi(), atoa() and gcvt() do? Show how u use them in a program. would yo

2] Does there exist any other function which can be used to convert an integer or a float to a string? If yes, show how you would use it. 3] es? How would you use qsort() function to sort an array of structur

4] How would you use qsort() function to sort the names stored in an array of pointers to strings? 5] How would you use bsearch() function to search a name stored in an array of pointers to strings? 6] 7] 8] 9] 10] 11] s? 12] 13] How would you use the function sin(), pow(), sqrt()? How would you use the function memcpy()? How would you use the function memset()? How would you use the function memmove()? How would you use the functions fseek(), fread(), fwrite() and ftell()? How would you obtain the current time and difference between two time

How would you use the function randomize() and random()? Would the following program always output 'Bangalore'? main() { char str1[] = "Bangalore - 440010"; char str2[10]; strncpy(str2,str1,8); printf("\n %s",str2); }

{24.dd[i].m.dd[i].random() 19] What's the difference between the functions .14] Can you shorten this code? main() { char str1[] = "Bangalore . clrscr (). inti.440010". printf("%s". str2[0] = '\0'.str1. {16.dd[i].y. for(i=0. main() { struct date dd[]={ {17.const void*). } c 18] How should we sort a linked list? rand().w. } 15] How would you implement a substr() function that extracts a g from a given string? substrin 16] Given a sentence containing words separated by spaces how would you construct an array of pointers to strings containing addresses of eac h word in the sentance? 17] Write the comparison function qs_compare()for the following ode struct date { int d.w.8.m. qsort(dd.srand() and randomise()? .i<4.62}. qs_compare(const void*.qs_compare).2. {17. strncat(str2.8).5.11.11.str2).78}. }.y). {19.94} }.76}. char str2[10].12.i++) printf("\n%d%d%d".62}. w=sizeof(struct date).

string). } 2] The function sprintf() can be used for this purpose. itoa() converts an integer to a string.string.4.i). i = atoi(s). .string[20]. This function also has the ability to format the numbers as they are converted to str ings. int i.20] What's the difference between the funtion memmove() and memcpy()? 21] How would you print a string on the printer? 22] Can you use the funtion fprintf()to display the output on the screen? *********************************************************************** ********************** ANSWERS *********************** *********************************************************************** 1] atoi() converts a string to an integer.141672. # include"stdlib. gcvt() converts a floating-point number to a string. printf("\n %d". The following program shows how to use this function.buffer).h" main() { char s[] = "12345".buffer). gcvt(20. char buffer[15].2). printf("\n %s". printf("\n %s". itoa(15.

4.name.ss[x].h" # inlucde"stdlib.80.ss[x].h" struct stud { int rollno. int sort_marks(struct stud *.name.96. {2. char name[30].85.sort_name). } 3] # include"string.struct stud *).ss[x]. printf("\n\n In order of marks:"). qsort(ss.sort_m). qsort(ss.h" main() { int a = 25. w = sizeof(struct stud). int sort_m(struct stud *. puts(str).sort_marks). sprintf(str.4. {8.ss[x]. x++) printf("\n %d %s %d". for(x = 0.4.rollno. x++) printf("\n %d %s %d".ss[x].b). x++) printf("\n %d %s %d".w.rollno.struct stud *)."a = %d b = %f".a. }. x < 4."Sushmita"}.w. }. main() { statis struct stud ss[] = { {15. x < 4.ss[x]. clrscr(). for(x = 0. int x."Madhuri"}.name. x < 4. printf("\n\n In order of names:"). char str[40].rollno.ss[x].97.w.marks). float b = 3."Akshay"}.struct stud *).ss[x].w. int marks.ss[x].Aishvarya"}. qsort(ss.14. } . for(x = 0. int sort_name(struct stud *.marks). printf("\n In order of roll numbers:").# include"stdio. {10.marks).

t2->marks).names[i]). return(strcmp(*t11. sort_name).sizeof(char *). } int sort_marks(struct stud *t1. main() { char *names[] = { .struct stud *t2) { return(strcmp(t1->name .h" int sort_name(const void * . for(i = 0.struct stud *t2) { return(t1->marks .h" # include"stdlib. **t22. "Madhuri". } 4] # include"string. /* cast appropriately */ t11 = (char **)t1.const void * ).t2->name)).int sort_m(struct stud *t1.char **). qsort(names. main() { char *names[] = { "Akshay". } int sort_name(struct stud *t1. i < 5. t22 = (char **)t2.t2->rollno). const void *).5. int bs_compare(char **. } 5] # include"string.h" # include"stdlib. int i. "Aishvarya".struct stud *t2) { return(t1->rollno . "Sudeepta".h" int sort_name(const void *.*t22)). i++) printf("\n %s". "Sushmita". const void *t2) { /* t1 and t2 are always pointers to objects being compared */ char **t11. }. } int sort_name(const void *t1.

if(b == NULL) printf("Not found"). "Sushmita".2). *s2)). b = bsearch(&s. .names[i]). const void *t2) { /* t1 and t2 are always pointers to objects being compared */ char **t11.y). bs_compare). nel. scanf("%d".sort_name). y = sqrt(b). names. for(i = 0. nel = sizeof(names) / wide. c = sin(angrad).char **s2) { return(strcmp(*s1. } int sort_name(const void *t1. }. printf("\nEnter the angle in degrees").h" main() { int ang.*t22)). wide. qsort(names.b. clrscr(). wide = sizeof(names[0]). i++) printf("\n %s". } 6] # include"math. printf("\n cosine of angle %d is: %d". **b. char *s = "aMadhuri". /* cast appropriately */ t11 = (char **)t1.nel. "Aishvarya".5.a.sizeof(char *).14/ 180.wide.ang. **t22. b = 1 .y."Akshay".a. "Madhuri". float angrad. return(strcmp(*t11.&and). } int bs_compare(char **s1. t22 = (char **)t2.*b). "Sudeepta".x. i < 5. a = pow(x. angrad = ang * 3. int i. else pritf("\n\n %s".

} 10] # include"stdio. Madam". area = sizeof(src). but for stronger back".h" struct stud { int rollno.src). area = sizeof(src).src). char src[] = "Bon jour. printf("\n %s".area . char src[] ="Life is the camera and you are the target" "so keep smiling always". char *dest. .area).area). printf("\n %s".src.h" main() { int area. char scr[]="Pray.dest).src. memset(src. dest = malloc(area). printf("\n %s". } 9] # include"mem.h" main() { int area.h" # include"alloc.src). printf("\n %s". mamcpy(dest. dest = malloc(Area).dest). printf("\n %s".} 7] # include"math. } 8] # include"mem. memmove(dest.h" # include"alloc.7). area = sizeof(Src). float per."!".h" main() { int area. char name[10]. not for lighter load. char *dest.

fwrite(&e.}e.rollno == rollno) { printf("\n Enter the new record").fs) == 1) { if(e.t2.dat". int rollno. time(&t2). int i = 2. } do { printf("\n Enter code no. scanf("%d". time(&t1).fs). exit(1).per temp.t1). char ch.e. }while(ch == 'Y').h" main() { time_t t1. main() { long position = OL. while(fread(&e.&rollno)."rb+"). printf("\n Program was active for %lf seconds". if(fs == NULL) { puts("Unable to open file"). } 11] # include"time. &temp). FILE *fs.diff).position.h" # include"dos. fs = fopen("stud. } puts("You want to modify records").sizeof(e). dcanf("%s %f". sleep(i). } position = ftell(fs). diff = difftime(t2. fseek(fs. } .f.sizeof(e). break.1. float temp. ch = getche(). SEEK_SET). e.1.name. double diff. to modify:").

str1.s + pos.str2). strncat() always terminates the target string with a '\0'.8). using sprintf() as shown below: main() { char str1[] = "Bangalore-440010". printf("%s".3). nosound(). 14] Yes.h" .str1.12] # include"dos.1. printf("\n Press any key to stop.int len) { t[0] = '\0'.str1). A better way of co pying would be: str[2] = '\0'. } 15] main() { char str1[] = "Bangalore". char str2[10].8."%.int pos. } } 13] No.char *s.len). /* Extract 3 characters beginning with first character */ sprintf(str2.h" main() { randomize(). delay(random(100)). strncat(str2."). char str2[5].h" # inlcude"stdlib. } 16] # include"stdio.*s". printf("%s". strncat(t. } substr(char *t. while!kbhit()) { sound(random(500)). sprintf(str2.h" # include"string.str2). Because after copying the source string into the target string s trncpy() doesn't terminate the target string with a '\0'.

fill it with pointers to the various nod es in the list. 19] rand() random() : returns a random number. } } } for(j = 0. else return(0). j++) printf("\n %s". j < i. else if(sp1->m < sp2->m) return(-1). : retruns a random number in a specified range. char *ptr. call qsort() and finally rebuild the list pointers based on t he sorted array. else if(sp1->y > sp2->y) return(1).const void *p2) { const struct date *sp1 = p1. else if(sp1->d < sp2->d) return(-1). if(p == NULL) break. } 18] Often it's easier to keep the list in order as we build it rather t han sorting it later. while(1) { p = strtok(NULL. i++." ").main() { char str[] = "This is a test". char *p. else if(sp1->m > sp2->m) return(1). else if(sp1->d > sp2->d) return(1). . int i = 1.j. const struct date *sp2 = p2. Still if we want to sort the list then we can allo cate a temporary array of pointers. } 17] int qs_compare(const void *p1." "). p = strtok(str. if(p != NULL) { [tr[0] = p. if(sp1->y < sp2->y) return(-1).ptr[j]). else { ptr[i] = p.

i. randomize : initialises a random number genenrator with a random value based on time. 21] # include"stdio.h" main() { char str[] = "There wouldn't have been COBOL without C". fprintf(stdprn. below: 22] .str. if source and destination overlaps the behaviour of memcpy() is undefined whereas memmove() carries out the copying correctly.a)."\n %s \n". by replacing the usual file pointer with stdout as shown fprintf(stdout.srand() : initialses a random number generator with a given seed value. memcpy() is m ore efficient. 20] Both the functions copy a block of bytes from source to destinat ion. However."%s %d %f". } Yes.str). whereas memmove() is safer to use.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->